Hi,
I want to connect to a Access 2003 database using VB 6 and then by clicking on a command button I want to run an sql query from a specified attribute in a specified table from the Access 2003 database. The problem is there are 4 different text boxes on the form and I want a query to be run on a different attribute in the same table depending which text box has data entered into it. I want this all to run from the same command button and I want any search results from the queries to be displayed in a list box. Any help for the code for this would be appreciated. I also don't know any of the code to connect to the database. I would also appreciate it if you could tell me under what sub routines each piece of code would go under. From reading tutorials I have come up with this code and I'm sure I'm close with it:
Dim ADOCn As ADODB.Connection
Dim ConnString As String
Dim adoRS As ADODB.Recordset
Dim sSQL As String
ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=c:\YourDB.mdb;" & _
"Persist Security Info=False"
Set ADOCn = New ADODB.Connection
ADOCn.ConnectionString = ConnString
ADOCn.Open ConnString
Set adoRS = New ADODB.Recordset
sSQL = "SELECT fieldname FROM tablename WHERE fieldname = '" & Text1.Text & "'"
adoRS.Open sSQL, ADOCn
Text2.Text = adoRS.Fields.Item("fieldname").Value
ADOCn.Close
Set ADOCn = Nothing
Set adoRS = Nothing
End Sub
Any help with fixing this code would be very helpful because I've been working on it for hours now and I'm starting to tear my hair out.
Thanks,
Darren
I want to connect to a Access 2003 database using VB 6 and then by clicking on a command button I want to run an sql query from a specified attribute in a specified table from the Access 2003 database. The problem is there are 4 different text boxes on the form and I want a query to be run on a different attribute in the same table depending which text box has data entered into it. I want this all to run from the same command button and I want any search results from the queries to be displayed in a list box. Any help for the code for this would be appreciated. I also don't know any of the code to connect to the database. I would also appreciate it if you could tell me under what sub routines each piece of code would go under. From reading tutorials I have come up with this code and I'm sure I'm close with it:
Dim ADOCn As ADODB.Connection
Dim ConnString As String
Dim adoRS As ADODB.Recordset
Dim sSQL As String
ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=c:\YourDB.mdb;" & _
"Persist Security Info=False"
Set ADOCn = New ADODB.Connection
ADOCn.ConnectionString = ConnString
ADOCn.Open ConnString
Set adoRS = New ADODB.Recordset
sSQL = "SELECT fieldname FROM tablename WHERE fieldname = '" & Text1.Text & "'"
adoRS.Open sSQL, ADOCn
Text2.Text = adoRS.Fields.Item("fieldname").Value
ADOCn.Close
Set ADOCn = Nothing
Set adoRS = Nothing
End Sub
Any help with fixing this code would be very helpful because I've been working on it for hours now and I'm starting to tear my hair out.
Thanks,
Darren