Hi everyone. I'm building a vehicle bookings database and am currently trying to use some VB code to control the data.
I have a
Bookings form, a
Vehicles table and a
Bookings table. I want to enter information into the Bookings form and have it write to the Bookings table. I'm using some VB code I found to allow users to add new vehicles, i.e. when someone types a vehicle name which isn't in the Vehicles table, it adds it to the Vehicles table,
then inserts the full booking into the Bookings table.
This is the code:
Code:
Private Sub ComboVehicle_NotInList(NewData As String, Response As Integer)
'Allow user to save non-list items.
Dim cnn As New ADODB.Connection
Dim strSQL As String
Dim bytUpdate As Byte
On Error GoTo ErrHandler
Set cnn = CurrentProject.Connection
bytUpdate = MsgBox("Do you want to add " & ComboVehicle.Value & _
" as a new vehicle?", vbYesNo, "Vehicle not found")
If bytUpdate = vbYes Then
strSQL = "INSERT INTO Vehicles(Vehiclename) " & "VALUES ('" & NewData & "')"
Debug.Print strSQL
cnn.Execute strSQL
Response = acDataErrAdded
ElseIf bytUpdate = vbNo Then
Response = acDataErrContinue
Me!ComboVehicle.Undo
End If
Exit Sub
ErrHandler:
MsgBox Err.Number & ": " & Err.Description, vbOKOnly, "Error"
End Sub
The problem I'm having is with this line:
Code:
bytUpdate = MsgBox("Do you want to add " & ComboVehicle.Value & _
" as a new vehicle?", vbYesNo, "Vehicle not found")
It seems "ComboVehicle.Value" is supposed to pull the current contents of the ComboVehicle dropdown box, but it doesn't seem to update. E.g. if you drop the box down and select "merc" but then decide to delete "merc" and type a new vehicle "atego" then press enter, the box pops up and says "Vehicle not found. Do you want to add
merc as a new vehicle?". It would, however, add
atego to the database, it's just displaying the old value.
Does anyone know if there's any way around this? I sort of understand what it's doing but I don't want my users to be confused!
Thanks,
valoukh.