Pauline123
Registered User.
- Local time
- Today, 07:33
- Joined
- Apr 1, 2013
- Messages
- 69
HI, I am stuck - I would like to update a table with data from a form.
I initially used a bound form and placed a subform in a tab control on the main form but this failed to work.
Have now considering using a pop-up unbound form with a command button when clicked with update the table.
I found this code but am having trouble getting it go.
My table is = ChecklistDetailsTB
My unbound form is = ChecklistUpdateForm
with fields: txtRegistration / txtWE / txtDriver / txtMileageTD
Hope someone can help
Pauline
Dim db As Database
Dim rec As Recordset
Set db = CurrentDb
'Check that all fields are filled in
txtRegistration.SetFocus
If txtRegistration.Text = "" Then
err = err + 1
MsgBox "Please select the Registration!" & err
End If
txtWE.SetFocus
If txtWE.Text = "" Then
err = err + 1
MsgBox "Please fill in the the WE Date!"
End If
txtDriver.SetFocus
If txtDriver.Text = "" Then
err = err + 1
MsgBox "Please fill in Driver's Name!"
End If
txtMileageTD.SetFocus
If txtMileageTD.Text = "" Then
err = err + 1
MsgBox "Please fill in the current mileage!"
End If
'if no errors insert data
If err < 1 Then
' Open a connection.
Set cnn1 = New ADODB.Connection
mydb = "C:\contacts.mdb"
strCnn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mydb
cnn1.Open strCnn
' Open contact table.
Set rstVehicleCheclists = New ADODB.Recordset
rstVehicleCheclists.CursorType = adOpenKeyset
rstVehicleCheclistsB.LockType = adLockOptimistic
rstVehicleCheclists.Open "ChecklistDetailsTB", cnn1, , , adCmdTable
'get the new record data
rstcontact.AddNew
rstVehicleCheclists!Registration = txtRegistration
rstVehicleCheclists!WE = txtWE
rstVehicleCheclists!Driver = txtDriver
rstVehicleCheclists!MileageTD = txtMileageTD
rstVehicleCheclists!Actioned = txtActioned
rstVehicleCheclists.Update
' Show the newly added data.
MsgBox "New contact: " & rstVehicleCheclists!Registration & " has been successfully added"
'close connections
rstcontact.Close
cnn1.Close
Else
MsgBox "An Error has occurred, please check and try again"
End If
End Sub
I initially used a bound form and placed a subform in a tab control on the main form but this failed to work.
Have now considering using a pop-up unbound form with a command button when clicked with update the table.
I found this code but am having trouble getting it go.
My table is = ChecklistDetailsTB
My unbound form is = ChecklistUpdateForm
with fields: txtRegistration / txtWE / txtDriver / txtMileageTD
Hope someone can help

Dim db As Database
Dim rec As Recordset
Set db = CurrentDb
'Check that all fields are filled in
txtRegistration.SetFocus
If txtRegistration.Text = "" Then
err = err + 1
MsgBox "Please select the Registration!" & err
End If
txtWE.SetFocus
If txtWE.Text = "" Then
err = err + 1
MsgBox "Please fill in the the WE Date!"
End If
txtDriver.SetFocus
If txtDriver.Text = "" Then
err = err + 1
MsgBox "Please fill in Driver's Name!"
End If
txtMileageTD.SetFocus
If txtMileageTD.Text = "" Then
err = err + 1
MsgBox "Please fill in the current mileage!"
End If
'if no errors insert data
If err < 1 Then
' Open a connection.
Set cnn1 = New ADODB.Connection
mydb = "C:\contacts.mdb"
strCnn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mydb
cnn1.Open strCnn
' Open contact table.
Set rstVehicleCheclists = New ADODB.Recordset
rstVehicleCheclists.CursorType = adOpenKeyset
rstVehicleCheclistsB.LockType = adLockOptimistic
rstVehicleCheclists.Open "ChecklistDetailsTB", cnn1, , , adCmdTable
'get the new record data
rstcontact.AddNew
rstVehicleCheclists!Registration = txtRegistration
rstVehicleCheclists!WE = txtWE
rstVehicleCheclists!Driver = txtDriver
rstVehicleCheclists!MileageTD = txtMileageTD
rstVehicleCheclists!Actioned = txtActioned
rstVehicleCheclists.Update
' Show the newly added data.
MsgBox "New contact: " & rstVehicleCheclists!Registration & " has been successfully added"
'close connections
rstcontact.Close
cnn1.Close
Else
MsgBox "An Error has occurred, please check and try again"
End If
End Sub