Welcome to Dream.In.Code
Getting VB Help is Easy!

Join 136,182 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 2,052 people online right now. Registration is fast and FREE... Join Now!




MS Access to SQL

2 Pages V  1 2 >  
Reply to this topicStart new topic

MS Access to SQL

enteng
4 Sep, 2007 - 05:26 AM
Post #1

D.I.C Head
**

Joined: 2 Sep, 2007
Posts: 56



Thanked: 1 times
My Contributions
I have a code here for login but using MS Access as backend, can you help me convert it to SQL as my back end

here it is:

CODE
Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class frmLogin
    Inherits System.Windows.Forms.Form
    
    Private Sub Command2_Click()
        
    End Sub
    
    
    Private Sub cmdCancel_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdCancel.Click
        Me.Close()
    End Sub
    
    Private Sub cmdOk_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdOk.Click
        Dim db_file As String
        Dim statement As String
        Dim conn As ADODB.Connection
        Dim rs As ADODB.Recordset
        
        ' Open the database.
        db_file = My.Application.Info.DirectoryPath
        If VB.Right(db_file, 1) <> "\" Then db_file = db_file & "\"
        db_file = db_file & "data.mdb"
        
        ' Open a connection.
        conn = New ADODB.Connection
        conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & db_file & ";" & "Persist Security Info=False"
        conn.Open()
        
        ' Look up the user name/password.
        statement = "SELECT COUNT (*) FROM Users WHERE " & "UserName='" & Replace(txtUserName.Text, "'", "''") & "' AND " & "Password='" & Replace(txtPassword.Text, "'", "''") & "'"
        rs = conn.Execute(statement)
        
        ' See if we got anything.
        If CInt(rs.Fields(0).Value) < 1 Then
            ' There is no match.
            ' Do not allow the login.
            Me.Close()
            MsgBox("Invalid user name/password.")
        Else
            ' There is a match.
            ' Display the program's main form.
            Form1.Show()
            Me.Close()
        End If
        
        rs.Close()
        conn.Close()
    End Sub
End Class


thank you

This post has been edited by enteng: 4 Sep, 2007 - 05:35 AM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: MS Access To SQL
4 Sep, 2007 - 08:18 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,199



Thanked: 213 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
First of all, in the future you might want to post in the VB.NET forum so you can get a more accurate answer in a timely manner. This forum is for VB previous to .NET.

As for your question, the conversion is not that hard if you know where to start looking for the information. What you will want to check out is a "SQLDataAdapter", "SQLConnection", and possibly "DataReaders" which will allow you to connect and read with little editing of this code. Below I have put a link to a great site that explains a few of the parts and provides some sample code that you can adapt to your particular situation.

I hope you find this useful.

Using DataReaders and SQL Server in VB.NET

User is offlineProfile CardPM
+Quote Post

enteng
RE: MS Access To SQL
4 Sep, 2007 - 08:56 AM
Post #3

D.I.C Head
**

Joined: 2 Sep, 2007
Posts: 56



Thanked: 1 times
My Contributions
thank you martyr2, sorry for wrong post.

i'll try your link.
User is offlineProfile CardPM
+Quote Post

AdmSteck
RE: MS Access To SQL
4 Sep, 2007 - 09:00 AM
Post #4

D.I.C Head
**

Joined: 26 Nov, 2005
Posts: 57


My Contributions
If I'm not mistaken, you should still be able to use ADODB objects to query a sql database. All you should have to do is change the connection string. Do a google search for 'sql connection string' and you should get quite a few examples.
User is offlineProfile CardPM
+Quote Post

enteng
RE: MS Access To SQL
4 Sep, 2007 - 09:29 AM
Post #5

D.I.C Head
**

Joined: 2 Sep, 2007
Posts: 56



Thanked: 1 times
My Contributions
when i used the code from the link you had given me, there are errors appear

Error 1 Type 'System.Data.SqlClient.SqlDataReader' has no constructors. C:\Documents and Settings\Ericson Ramos\Local Settings\Application Data\Temporary Projects\WindowsApplication1\Form1.vb 8 9 WindowsApplication1

Error 2 Identifier expected. C:\Documents and Settings\Ericson Ramos\Local Settings\Application Data\Temporary Projects\WindowsApplication1\Form1.vb 11 83 WindowsApplication1
Error 3 Syntax error. C:\Documents and Settings\Ericson Ramos\Local Settings\Application Data\Temporary Projects\WindowsApplication1\Form1.vb 12 1 WindowsApplication1
Error 4 Variable 'e' hides a variable in an enclosing block. C:\Documents and Settings\Ericson Ramos\Local Settings\Application Data\Temporary Projects\WindowsApplication1\Form1.vb 32 15 WindowsApplication1


the code:

CODE
Imports System.Data.SqlClient
Imports System.Data.SqlClient.SqlDataReader

Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim myConnection As SqlConnection
    Dim myCommand As SqlCommand
    Dim dr As New SqlDataReader()
    'declaring the objects

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)_
Handles MyBase.Load
        myConnection = New SqlConnection("server=localhost;uid=sa;pwd=;database=pubs")
        'establishing connection. you need to provide password for sql server
        Try
            myConnection.Open()
            'opening the connection
            myCommand = New SqlCommand("Select * from discounts", myConnection)
            'executing the command and assigning it to connection
            dr = myCommand.ExecuteReader()
            While dr.Read()
                'reading from the datareader
                MessageBox.Show("discounttype" & dr(0).ToString())
                MessageBox.Show("stor_id" & dr(1).ToString())
                MessageBox.Show("lowqty" & dr(2).ToString())
                MessageBox.Show("highqty" & dr(3).ToString())
                MessageBox.Show("discount" & dr(4).ToString())
                'displaying the data from the table
            End While
            dr.Close()
            myConnection.Close()
        Catch e As Exception
        End Try
    End Sub

End Class

User is offlineProfile CardPM
+Quote Post

AdmSteck
RE: MS Access To SQL
4 Sep, 2007 - 10:07 AM
Post #6

D.I.C Head
**

Joined: 26 Nov, 2005
Posts: 57


My Contributions
Put a space between the )_ on line 11 to take care of the first two errors
CODE
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)_
Handles MyBase.Load

becomes
CODE
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load


Next change the variable in the catch block from e to ex so that you don't have two variables named e. (one for the event arguments and one for the exception)
CODE
Catch e As Exception

becomes
CODE
Catch ex As Exception

User is offlineProfile CardPM
+Quote Post

enteng
RE: MS Access To SQL
4 Sep, 2007 - 10:33 AM
Post #7

D.I.C Head
**

Joined: 2 Sep, 2007
Posts: 56



Thanked: 1 times
My Contributions
Thank you AdmSteck you give me direct hit smile.gif

unfortunately i still have error:

Error 1 Type 'System.Data.SqlClient.SqlDataReader' has no constructors. C:\Thesis\WindowsApplication1\WindowsApplication1\Form1.vb 8 9 WindowsApplication1

CODE
Imports System.Data.SqlClient
Imports System.Data.SqlClient.SqlDataReader


Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim myConnection As SqlConnection
    Dim myCommand As SqlCommand
    Dim dr As New SqlDataReader()
    'declaring the objects

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles MyBase.Load
        myConnection = New SqlConnection("server=localhost;uid=sa;pwd=;database=pubs")
        'establishing connection. you need to provide password for sql server
        Try
            myConnection.Open()
            'opening the connection
            myCommand = New SqlCommand("Select * from discounts", myConnection)
            'executing the command and assigning it to connection
            dr = myCommand.ExecuteReader()
            While dr.Read()
                'reading from the datareader
                MessageBox.Show("discounttype" & dr(0).ToString())
                MessageBox.Show("stor_id" & dr(1).ToString())
                MessageBox.Show("lowqty" & dr(2).ToString())
                MessageBox.Show("highqty" & dr(3).ToString())
                MessageBox.Show("discount" & dr(4).ToString())
                'displaying the data from the table
            End While
            dr.Close()
            myConnection.Close()
        Catch ex As Exception
        End Try
    End Sub

End Class


User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: MS Access To SQL
4 Sep, 2007 - 10:37 AM
Post #8

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,983



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
First, get rid of this line

CODE

Imports System.Data.SqlClient.SqlDataReader


When you're importing System.Data.SqlClient theres no need to add SqlDataReader to the list

Also change this line

CODE

Dim dr As New SqlDataReader()


to

CODE

Dim dr As SqlDataReader()


The SqlDataReader has no constructrs (like say a SqlDataConnection which can be Dim conn As New SqlDataConnection("YourConnectionString))
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: MS Access To SQL
4 Sep, 2007 - 10:45 AM
Post #9

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,199



Thanked: 213 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Try changing the line Dim dr As New SqlDataReader() to Dim dr As SqlDataReader instead and see if that helps. You don't need to create a new object of datareader there, it just needs to be of that type for when you use the ExecuteReader later on the command object.

That should help. smile.gif

Damn, beaten to the punch! and btw keep those parenthesis off if you are going to use the ExecuteReader later. Parenthesis are only needed for new objects. If you keep them on, it will start flagging your executereader line as bogus. wink2.gif

This post has been edited by Martyr2: 4 Sep, 2007 - 10:48 AM
User is offlineProfile CardPM
+Quote Post

enteng
RE: MS Access To SQL
4 Sep, 2007 - 10:49 AM
Post #10

D.I.C Head
**

Joined: 2 Sep, 2007
Posts: 56



Thanked: 1 times
My Contributions
after removing the Imports System.Data.SqlClient.SqlDataReader and change the line dr As New ... to dr As Sql...

i encounter dis error

Error 1 Value of type 'System.Data.SqlClient.SqlDataReader' cannot be converted to '1-dimensional array of System.Data.SqlClient.SqlDataReader'. C:\Thesis\WindowsApplication1\WindowsApplication1\Form1.vb 20 18 WindowsApplication1

Error 2 'Read' is not a member of 'System.Array'. C:\Thesis\WindowsApplication1\WindowsApplication1\Form1.vb 21 19 WindowsApplication1

Error 3 'Close' is not a member of 'System.Array'. C:\Thesis\WindowsApplication1\WindowsApplication1\Form1.vb 30 13 WindowsApplication1

CODE
Imports System.Data.SqlClient


Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim myConnection As SqlConnection
    Dim myCommand As SqlCommand
    Dim dr As SqlDataReader()
    'declaring the objects

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles MyBase.Load
        myConnection = New SqlConnection("server=localhost;uid=sa;pwd=;database=pubs")
        'establishing connection. you need to provide password for sql server
        Try
            myConnection.Open()
            'opening the connection
            myCommand = New SqlCommand("Select * from discounts", myConnection)
            'executing the command and assigning it to connection
            dr = myCommand.ExecuteReader()
            While dr.Read()
                'reading from the datareader
                MessageBox.Show("discounttype" & dr(0).ToString())
                MessageBox.Show("stor_id" & dr(1).ToString())
                MessageBox.Show("lowqty" & dr(2).ToString())
                MessageBox.Show("highqty" & dr(3).ToString())
                MessageBox.Show("discount" & dr(4).ToString())
                'displaying the data from the table
            End While
            dr.Close()
            myConnection.Close()
        Catch ex As Exception
        End Try
    End Sub

End Class


User is offlineProfile CardPM
+Quote Post

Martyr2
RE: MS Access To SQL
4 Sep, 2007 - 10:51 AM
Post #11

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,199



Thanked: 213 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Read the edits I had made to my last post. Drop those parenthesis on the dim line for sqldatareader. The parenthesis make it think you are trying to establish a new object.
User is offlineProfile CardPM
+Quote Post

enteng
RE: MS Access To SQL
4 Sep, 2007 - 10:53 AM
Post #12

D.I.C Head
**

Joined: 2 Sep, 2007
Posts: 56



Thanked: 1 times
My Contributions
Great help both of you Martyr2 and psychocoder

thank you very much
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 01:11AM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month