Values are assigned to named arguments with the := operator not the =
operator, i.e.
DoCmd.OpenForm "frmBargeNamesEntry", Datamode:=acFormAdd,
WindowMode:=acDialog, OpenArgs:=NewData
What's happening in your case is that the form is opening, but not in
dialogue mode, so code execution is not being interrupted and is passing
almost immediately to the line:
DoCmd.Close acForm, "frmBargeNamesEntry"
which closes the form.
Here's a another example of this type of code (error handling omitted),
which differs slightly from yours in that before requerying the combo box it
checks to see that the new value has been added using the DLookup function:
Private Sub cboCities_NotInList(NewData As String, Response As Integer)
Dim ctrl As Control
Dim strMessage As String
Set ctrl = Me.ActiveControl
strMessage = "Add " & NewData & " to list?"
If MsgBox(strMessage, vbYesNo + vbQuestion) = vbYes Then
DoCmd.OpenForm "frmCities", _
DataMode:=acFormAdd, _
WindowMode:=acDialog, _
OpenArgs:=NewData
' ensure frmCities closed
DoCmd.Close acForm, "frmCities"
' ensure city has been added
If Not IsNull(DLookup("CityID", "Cities", "City = """ & _
NewData & """")) Then
Response = acDataErrAdded
Else
strMessage = NewData & " was not added to Cities table."
MsgBox strMessage, vbInformation, "Warning"
Response = acDataErrContinue
ctrl.Undo
End If
Else
Response = acDataErrContinue
ctrl.Undo
End If
End Sub
The Open event procedure of the frmCities form includes the following code:
Private Sub Form_Open(Cancel As Integer)
If Not IsNull(Me.OpenArgs) Then
Me.City.DefaultValue = """" & Me.OpenArgs & """"
End If
End Sub
Note that this sets the DefaultValue property of the City control on the
frmCities form, not its Value property. This means that a new record is not
initiated until the user adds the other data for the city, in this example
the County in which it is located. Consequently the user can back out of the
form without adding a new record if they wish.
Ken Sheridan
Stafford, England
tankerman said:
Tom, (2003 is what I'm using) I used cbo TomsMethod you posted, I changed the
frm names, copied the Function IsLoaded2 to a module. I changed my Limit To
List on my cmbox to YES. I tried it out by putting in a barge name that I
knew was not in the list and here's what I got. When I tabbed to the next
field a box pop up telliing me that this barge name was not found, do you
wish to add this barge name? YES or NO. When I clicked YES my
frmBargeNamesEntry form pops up but just for a flash and then gone and the
message not on list pops up. Here is what I put in the On Not on List of my
combo box
Private Sub Combo19_NotInList(NewData As String, Response As Integer)
On Error GoTo ProcError
Dim strResponse As String
strResponse = MsgBox("Barge Name not found" & vbCrLf & "Do you wish to add
this Barge Name?", vbYesNo + vbInformation, "Please Respond")
If strResponse = vbYes Then
DoCmd.OpenForm "frmBargeNamesEntry", Datamode = acFormAdd, WindowMode =
acDialog, OpenArgs = NewData
If IsLoaded2("frmBargeNamesEntry") Then
Response = acDataErrAdded
DoCmd.Close acForm, "frmBargeNamesEntry"
Else
Response = acDataErrContinue
End If
Else
Resonse = acDataErrContinue
End If
ExitProc:
Exit Sub
ProcError:
MsgBox "Error " & Err.Number & ": " & Err.Description
Resume ExitProc
End Sub
Tom Wickerath said:
Hi Tankerman,
... ect. if a product is not on the list in the drop down box I have a button on
the form to open up frmProductEntry so I can add a new product to my
tblProducts.
Rather than use a button, have you tried using the NotInList event procedure
for the combo box? You didn't state which version of Access that you are
using. If you happen to be using Access 2007, then you can achieve this goal
without VBA code.
Assuming you are using Access 2003 or lesser version, you can use VBA code
in the NotInList event procedure. I have an older sample here that you are
welcome to download:
http://www.seattleaccess.org/downloads.htm
See the following entry towards the bottom of the page:
"Not In List - Detailed Instructions by Tom Wickerath,
Download (16 kb)"
Also, Access MVP Allen Browne offers a web page with the same topic. His
covers the easier method for Access 2007, as well:
Adding values to lookup tables
http://allenbrowne.com/ser-27.html
Problem, how do I get my combo box to update without having to
close my frmEntry form.
Use the requery method: Me.NameOfComboBox.Requery
I tried to add a refresh button but I have a VB on the TicketNo field for
when a duplicate TicketNo is entered and it won't work properly.
"...it won't work properly" is not exactly enough information for us to
provide meaningful help. What does it do or not do? Have you set a break
point and stepped through the code one line at a time?
Tom Wickerath
Microsoft Access MVP
https://mvp.support.microsoft.com/profile/Tom
http://www.access.qbuilt.com/html/expert_contributors.html
__________________________________________
tankerman said:
I have a form "frmEntry" based off of a table that has several relationships.
4 combo boxes based off of queries so that I can select a product, dock code
ect. if a product is not on the list in the drop down box I have a button on
the form to open up frmProductEntry so I can add a new product to my
tblProducts. Problem, how do I get my combo box to update without having to
close my frmEntry form.
I tried to add a refresh button but I have a VB on the TicketNo field for
when a duplicate TicketNo is entered and it won't work properly. Here is that
VB
Private Sub TicketNo_BeforeUpdate(Cancel As Integer)
Dim db As DAO.Database
Set db = CurrentDb
Dim z As Variant
Dim v As Variant
z = DLookup("[TicketNo]", "[tblEventsDetails]", "[TicketNo]= '" &
Me.[TicketNo].Value & "'")
If Not IsNull(z) Then
v = MsgBox("This Ticket Number has already been Entered, Would you like
to edit this record? ", vbYesNo, "Duplicate Number Entered")
If v = vbYes Then
Dim rs As Object
Set rs = Me.RecordsetClone
rs.FindFirst "[TicketNo]= '" & z & " '"
If Not rs.NoMatch Then
Me.Undo
Me.Bookmark = rs.Bookmark
End If
rs.Close
Set rs = Nothing
Else
Me.[TicketNo].Undo
MsgBox "Please enter a new Ticket Number"
End If
Cancel = True
End If
End Sub
Any help would be appreciated.