G
Guest
I have been working on this for 3 days now and I have reasearched and tried
everything I could find.
Because of a bug in DAO (see http://support.microsoft.com/kb/242459/en-us) I
cannot add a foreign key to a table using a query or DAO VBA.
This is a very important part of a database I am developing and it needs to
be done without the user having to enter it.
So I have been forced to try ADO. I regularly use DAO recordsets but this
is my first attempt at ADO. I have tried all I could find on the web and in
books, but I have been unable to open an ADO recordset. Here is my
code--advice is welcomed.
Private Sub cmd1Test_Click
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim strSql As String
Dim lngHHID As Long
Dim lngPersonID As Long
lngHHID = Me.txtHouseholdID.Value
lngPersonID = Me.cboHHMemberName3.Value
Set cnn = New ADODB.Connection
Set cnn = CurrentProject.Connection
' Open the connection.
' With cnn
' .Provider = "Microsoft.Jet.OLEDB.4.0"
' .Open "Data Source=C:\Program Files\CMSS\CMSS_JBC_SPLIT_DB1.mdb"
' End With
strSql = "SELECT PersonID, HouseholdID FROM tblPeople" & _
" Where PersonID = Forms!frmHouseholds!cboHHMemberName3;"
Set rst = New ADODB.Recordset
With rst
.Open Source:=strSql, _
ActiveConnection:=cnn, _
CursorType:=adOpenKeyset, _
LockType:=adLockOptimistic
Do While Not .EOF
If .Fields("PersonID").Value = lngPersonID Then
.Fields(HouseholdID).Value = lngHHID
' Save the changes you made to the current record in the
Recordset object.
.Update
.MoveNext
End If
Loop
' Close the Recordset object.
.Close
End With
' Close connection and destroy object variables.
cnn.Close
Set rst = Nothing
Set cnn = Nothing
End Sub
As I stepped through the code the variables had the correct values, the
connection was open, but I could not open the recordset. There was an error
at that line every time.
Thanks in advance for your help.
Hunter57
everything I could find.
Because of a bug in DAO (see http://support.microsoft.com/kb/242459/en-us) I
cannot add a foreign key to a table using a query or DAO VBA.
This is a very important part of a database I am developing and it needs to
be done without the user having to enter it.
So I have been forced to try ADO. I regularly use DAO recordsets but this
is my first attempt at ADO. I have tried all I could find on the web and in
books, but I have been unable to open an ADO recordset. Here is my
code--advice is welcomed.
Private Sub cmd1Test_Click
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim strSql As String
Dim lngHHID As Long
Dim lngPersonID As Long
lngHHID = Me.txtHouseholdID.Value
lngPersonID = Me.cboHHMemberName3.Value
Set cnn = New ADODB.Connection
Set cnn = CurrentProject.Connection
' Open the connection.
' With cnn
' .Provider = "Microsoft.Jet.OLEDB.4.0"
' .Open "Data Source=C:\Program Files\CMSS\CMSS_JBC_SPLIT_DB1.mdb"
' End With
strSql = "SELECT PersonID, HouseholdID FROM tblPeople" & _
" Where PersonID = Forms!frmHouseholds!cboHHMemberName3;"
Set rst = New ADODB.Recordset
With rst
.Open Source:=strSql, _
ActiveConnection:=cnn, _
CursorType:=adOpenKeyset, _
LockType:=adLockOptimistic
Do While Not .EOF
If .Fields("PersonID").Value = lngPersonID Then
.Fields(HouseholdID).Value = lngHHID
' Save the changes you made to the current record in the
Recordset object.
.Update
.MoveNext
End If
Loop
' Close the Recordset object.
.Close
End With
' Close connection and destroy object variables.
cnn.Close
Set rst = Nothing
Set cnn = Nothing
End Sub
As I stepped through the code the variables had the correct values, the
connection was open, but I could not open the recordset. There was an error
at that line every time.
Thanks in advance for your help.
Hunter57