G
Guest
I am coding a form for a database of servers. The form is bound to the
table "Server Inventory." I have a combo box (combo35) from which a server
name (ServerName - Primary Key) is selected, and the form populates the text
boxes with all appropriate information.
The problem, however, is that I would like to be able to add servers on the
fly. I researched the topic, and it seemed the best way was to code this
into a NotInList event, and came up with the following code, and modified it
for my purposes. I am not experienced enough in VBA to know exactly how to
program these commands from scratch, but I am familiar enough with object
oriented programming concepts to modify the code.
Private Sub Combo35_NotInList(NewData As String, Response As Integer)
Dim db As DAO.Database
Dim rs As DAO.Recordset
If MsgBox("Add server " & NewData & " to server inventory?", vbQuestion
+ vbYesNo, "Add new name?") = vbNo Then
Response = acDataErrContinue
Else
Set db = CurrentDb
Set rs = db.OpenRecordset("Server Inventory", dbOpenDynaset)
On Error Resume Next
rs.AddNew
rs!ServerName = NewData
rs.Update
If Err Then
MsgBox "An error occurred. Please try again."
Response = acDataErrContinue
Else
Response = acDataErrAdded
End If
End If
rs.Close
Set rs = Nothing
Set db = Nothing
End Sub
As far as I am aware, this should work. However, I am not even seeing the
message box pop up, which leads me to believe that the event isn't even
firing. Perhaps the code in the combo35_AfterUpdate method is thowing it
off? If
anyone could lend a hand, it would be much appreciated.
table "Server Inventory." I have a combo box (combo35) from which a server
name (ServerName - Primary Key) is selected, and the form populates the text
boxes with all appropriate information.
The problem, however, is that I would like to be able to add servers on the
fly. I researched the topic, and it seemed the best way was to code this
into a NotInList event, and came up with the following code, and modified it
for my purposes. I am not experienced enough in VBA to know exactly how to
program these commands from scratch, but I am familiar enough with object
oriented programming concepts to modify the code.
Private Sub Combo35_NotInList(NewData As String, Response As Integer)
Dim db As DAO.Database
Dim rs As DAO.Recordset
If MsgBox("Add server " & NewData & " to server inventory?", vbQuestion
+ vbYesNo, "Add new name?") = vbNo Then
Response = acDataErrContinue
Else
Set db = CurrentDb
Set rs = db.OpenRecordset("Server Inventory", dbOpenDynaset)
On Error Resume Next
rs.AddNew
rs!ServerName = NewData
rs.Update
If Err Then
MsgBox "An error occurred. Please try again."
Response = acDataErrContinue
Else
Response = acDataErrAdded
End If
End If
rs.Close
Set rs = Nothing
Set db = Nothing
End Sub
As far as I am aware, this should work. However, I am not even seeing the
message box pop up, which leads me to believe that the event isn't even
firing. Perhaps the code in the combo35_AfterUpdate method is thowing it
off? If
anyone could lend a hand, it would be much appreciated.