IGBP
01-10-2004, 09:51 AM
Hi everyone ...
I posted on this last week, and have spent time finding/studying/doing tutorials on Databse access and ADO. I am still stuck and this seems it should be rather simple.
I am trying to do this only in code, not with any controls directly connected to the database(for this portion of the project), so I have most of the database connection stuff in a seperate .BAS module.
here is that code
EDIT: Using VB6 and Access2000
Option Explicit
Public cnMyCon As ADODB.Connection 'Connection to ADODB
Public MyRS As ADODB.Recordset 'Holds records
Public Sub ConnectToDBase(strSQL As String)
Dim lngRecordCount As Long
Set cnMyCon = New ADODB.Connection
Set MyRS = New ADODB.Recordset
MyRS.CursorLocation = adUseClient
cnMyCon.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Administrator\Desktop\test.mdb;Persist Security Info=False"
cnMyCon.CursorLocation = adUseClient
MyRS.Open strSQL, cnMyCon, adOpenStatic, adLockOptimistic
' MyRS.Open strSQL, cnMyCon, adOpenDynamic, adLockOptimistic
If Not MyRS.EOF Or Not MyRS.BOF Then
MyRS.MoveLast 'without these the recordcount
MyRS.MoveFirst 'ends up -1
End If
lngRecordCount = MyRS.RecordCount
Debug.Print "From Mod " & lngRecordCount 'visual proof that it has something
End Sub
That works great ... returns 14 records that I use to set the caption of dynamically created Option buttons. All that works perfect.
Now the problem .. and scenerio ..
the purpose of this particular routine is 2 fold ...
First display all the info.(in one table)on a particualr company based on the user selection.
Second, Update one column of that companies record(check the dropped checkbox in the Database table).
This is what I need to do ...
User selects an option button ..
I need to do a select statement that will return all fields from the database table where the CompanyName = the option button caption.
Display that info.
Do an update statement.
Every time I try, I either get no records returned or an error in the SQLstatement. :confused:
Here is that code
Private Sub Option1_Click(Index As Integer)
Dim strCoName As String
Dim strSQL As String
MyRS.Close ' Close the original RS returned.
strCoName = Option1(Index).Caption
strSQL = "Select * From Suppliers where CompanyName = '" & strCoName & "'"
'strSQL = "select * from Suppliers where Companyname like '" & strCoName & "'"
'strSQL = "Select * From Suppliers where CompanyName Like '" & strCoName & "'" & ";"
modTestConnect.ConnectToDBase (strSQL)
MyRS.Fields.Item("Dropped").Value = True
MyRS.Update
End Sub
When that routine is run, I get either no recordset or a syntax error.
Any ideas?
Thanks
Michael
I posted on this last week, and have spent time finding/studying/doing tutorials on Databse access and ADO. I am still stuck and this seems it should be rather simple.
I am trying to do this only in code, not with any controls directly connected to the database(for this portion of the project), so I have most of the database connection stuff in a seperate .BAS module.
here is that code
EDIT: Using VB6 and Access2000
Option Explicit
Public cnMyCon As ADODB.Connection 'Connection to ADODB
Public MyRS As ADODB.Recordset 'Holds records
Public Sub ConnectToDBase(strSQL As String)
Dim lngRecordCount As Long
Set cnMyCon = New ADODB.Connection
Set MyRS = New ADODB.Recordset
MyRS.CursorLocation = adUseClient
cnMyCon.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Administrator\Desktop\test.mdb;Persist Security Info=False"
cnMyCon.CursorLocation = adUseClient
MyRS.Open strSQL, cnMyCon, adOpenStatic, adLockOptimistic
' MyRS.Open strSQL, cnMyCon, adOpenDynamic, adLockOptimistic
If Not MyRS.EOF Or Not MyRS.BOF Then
MyRS.MoveLast 'without these the recordcount
MyRS.MoveFirst 'ends up -1
End If
lngRecordCount = MyRS.RecordCount
Debug.Print "From Mod " & lngRecordCount 'visual proof that it has something
End Sub
That works great ... returns 14 records that I use to set the caption of dynamically created Option buttons. All that works perfect.
Now the problem .. and scenerio ..
the purpose of this particular routine is 2 fold ...
First display all the info.(in one table)on a particualr company based on the user selection.
Second, Update one column of that companies record(check the dropped checkbox in the Database table).
This is what I need to do ...
User selects an option button ..
I need to do a select statement that will return all fields from the database table where the CompanyName = the option button caption.
Display that info.
Do an update statement.
Every time I try, I either get no records returned or an error in the SQLstatement. :confused:
Here is that code
Private Sub Option1_Click(Index As Integer)
Dim strCoName As String
Dim strSQL As String
MyRS.Close ' Close the original RS returned.
strCoName = Option1(Index).Caption
strSQL = "Select * From Suppliers where CompanyName = '" & strCoName & "'"
'strSQL = "select * from Suppliers where Companyname like '" & strCoName & "'"
'strSQL = "Select * From Suppliers where CompanyName Like '" & strCoName & "'" & ";"
modTestConnect.ConnectToDBase (strSQL)
MyRS.Fields.Item("Dropped").Value = True
MyRS.Update
End Sub
When that routine is run, I get either no recordset or a syntax error.
Any ideas?
Thanks
Michael