I am making a front end mdb so users can enter new members data.
I can then import this into the master database on the backend and erase the
existing info on the front end to keep the data reasonably secure.
( I borrowed the code from
http://www.databasedev.co.uk/unbound...-add-data.html example)
I have 4 tables that will be updated from this form:
Members
ContactInfo
Payments
History
I can get it to update the Members table with no problem, but it errors out
when we go beyond that.
I marked the spot where it errors out. The Members table gets updated but
not the rest.
Here's a look at the code:
-----------------------------------------------------------------
Private Sub cmdAddNewMember _Click()
Dim err As Integer
Dim cnn1 As ADODB.Connectio n
Dim rstcontact As ADODB.Recordset
Dim strCnn As String
'Check that all fields are filled in
FirstName.SetFo cus
If FirstName.Text = "" Then
err = err + 1
MsgBox "Please fill in the First Name box!" & err
End If
LastName.SetFoc us
If LastName.Text = "" Then
err = err + 1
MsgBox "Please fill in the Last Name box!" & err
End If
'if no errors insert data
If err < 1 Then
' Open a connection.
Set cnn1 = New ADODB.Connectio n
mydb = "U:\Apps\Office \AddNewMembers. mdb"
strCnn = "Provider=Micro soft.Jet.OLEDB. 4.0;Data Source=" & mydb
cnn1.Open strCnn
' Open Members table.
Set rstMembers = New ADODB.Recordset
rstMembers.Curs orType = adOpenKeyset
rstMembers.Lock Type = adLockOptimisti c
rstMembers.Open "Members", cnn1, , , adCmdTable
'get the new Members data
rstMembers.AddN ew
rstMembers!Firs tName = FirstName
rstMembers!Last Name = LastName
rstMembers.Upda te
' Show the newly added data.
MsgBox "New Members: " & rstMembers!Firs tName & "has been
successfully added"
'close connection to the Members table
rstMembers.Clos e
' *** THIS IS WHERE IT STOPS AND ERRORS OUT AFTER THIS LINE *
' Open ContactInfo table.
Set rstContactInfo = New ADODB.Recordset
rstContactInfo. CursorType = adOpenKeyset
rstContactInfo. LockType = adLockOptimisti c
rstContactInfo. Open "ContactInf o", cnn1, , , adCmdTable
'get the new record data
rstContactInfo. AddNew
rstContactInfo! Address1Line1 = Address1Line1
rstContactInfo! Address1Line2 = Address1Line2
rstContactInfo. Update
rstContactInfo. Close
' Open Status table.
Set rstStatus = New ADODB.Recordset
rstStatus.Curso rType = adOpenKeyset
rstStatus.LockT ype = adLockOptimisti c
rstStatus.Open "Status", cnn1, , , adCmdTable
'get the new Status data
rstStatus.AddNe w
rstStatus!Activ e = Active
rstStatus!Paid = Paid
rstStatus.Close
' Open Payments table.
Set rstPayments = New ADODB.Recordset
rstPayments.Cur sorType = adOpenKeyset
rstPayments.Loc kType = adLockOptimisti c
rstPayments.Ope n "Payments", cnn1, , , adCmdTable
'get the new Payments data
rstPayments.Add New
rstPayments!Pay mentType = PaymentType
rstPayments!Amo untPaid = AmountPaid
rstPayments!Mem bershipType = MembershipType
rstPayments.Clo se
' Open History table.
Set rstHistory = New ADODB.Recordset
rstHistory.Curs orType = adOpenKeyset
rstHistory.Lock Type = adLockOptimisti c
rstHistory.Open "History", cnn1, , , adCmdTable
'get the new History data
rstHistory.AddN ew
rstHistory!Hist oryYear = Year
rstHistory!Spon soredBy = SponsoredBy
rstHistory!Hist oryNotes = HistoryNotes
rstHistory.Clos e
'Close connection to the database after all updates to the tables
cnn1.Close
Else
MsgBox "An Error has occurred, please check and try again"
End If
End Sub