M
my-wings
I have a form with drop-down lists. I want to be able to start typing
something in the list and if it is "not in list" I would like to be asked to
add it to the list. I'm sure this has to come up all the time, so I googled
for some code and found the message below, but I think it might be from an
older version of Access (date posted: 2-21-99) because the debugger stops on
a few things. I am hoping that you can help me get this working, because I
would use it all the time!
Here is the complete google reference:
http://groups.google.com/[email protected]
Here is the message and code I copied and tried to use. (Questions follow):
~~~~~~~~~~~~~BEGIN COPYING~~~~~~~~~~
Here's some code to make your life easier:
Add the Sub call to the Item Not In List Event on your form. For example, if
the
Terms of Payment field on a vendor form is a List Box, add this code to the
event:
ThingNotInList "tTerms", "Terms", "Terms", NewData, Response
Where "tTerms" is the table name that contains the different payment terms
that
are currently on file
"Terms" is the name of the field that contains the description of
the
payment terms
"Terms" is the description of the field that will appear in the
prompt to save / abandon the new data
NewData is the new terms data that the user entered in the list
box
Response is the users decision to add or abandon the new payment
terms
Here is the structure of the Terms Table (tTerms)
TermsID AutoNumber
Terms Text 50
The primary key is TermsID
' Generic subroutine to add an entry to a table.
============================================================================
Sub ThingNotInList(pThingTableName As String, pThingFieldName As String,
pThingDesc As String, pNewThingName As String, pResponse As Integer)
' Arguments:
' pThingTableName => The name of the table into which the new record
will
be inserted
' pThingFieldName => The name of the field in pThingTableName into
which
the new data will be inserted
' pThingDesc => A text description of the data that is being handled:
"Employee Name", "Author", "Date of Birth", etc. This text will appear in
the
message box that prompts the user to add pThingFieldName to pThingTableName
' pNewThingName => The new data that will be inserted into the
pThingFieldName field. It's the info that the user typed into the list box
onthe
form.
' pResponse => The user's decision: Add the data or not:
DATA_ERRADDED
or DATA_ERRCONTINUE
' Bill Nicholson: (e-mail address removed)
Dim status As Integer
Dim mydb As Database
Dim thingRecordset As Recordset
Dim thingField As Field
Dim thingTblDef As TableDef
Dim thingSize As Long
status = MsgBox("The " + pThingDesc + " that you entered is not on file." +
vbNewLine + " Do you want to add it?", 4 + 32, "")
If status = 6 Then ' User clicked "Yes" - he wants to add the Thing
Set mydb = CurrentDb
' Look up the field size
Set thingTblDef = mydb.TableDefs(pThingTableName)
With thingTblDef
For Each thingField In .Fields
If thingField.Name = pThingFieldName Then
thingSize = thingField.Size
Exit For
End If
Next thingField
End With
Set thingRecordset = mydb.OpenRecordset(pThingTableName)
thingRecordset.AddNew
thingRecordset(pThingFieldName) = Mid$(pNewThingName, 1, thingSize)
thingRecordset.Update
thingRecordset.Move 0, thingRecordset.LastModified ' Go to new record
thingRecordset.Close
mydb.Close
pResponse = DATA_ERRADDED ' Access will add the data to the
combo
box
Else
pResponse = DATA_ERRCONTINUE ' User clicked "No" - he does not
want
to add the Thing
End If
End Sub
~~~~~~~~~~~~END COPYING~~~~~~~~~~~
So far, my debugger stopped on:
"thingSize = thingField.Size" as it didn't recognize ".Size" I changed that
to ".ActualSize" as my guess for the closest thing on the available list.
Next, it stopped on:
thingRecordset.Move 0, thingRecordset.LastModified ' Go to new record
It didn't recognize ".LastModified" I couldn't find any likely equivalent
for that. (I was sort of hoping it would just "work" since I'm pretty new at
this.) There is an example of ".LastModified" in the help file, but it
doesn't show up as an option in that drop down list that appears when you
type the "dot" after the word "thingRecordset"
Any help would be greatly appreciated.
Alice
something in the list and if it is "not in list" I would like to be asked to
add it to the list. I'm sure this has to come up all the time, so I googled
for some code and found the message below, but I think it might be from an
older version of Access (date posted: 2-21-99) because the debugger stops on
a few things. I am hoping that you can help me get this working, because I
would use it all the time!
Here is the complete google reference:
http://groups.google.com/[email protected]
Here is the message and code I copied and tried to use. (Questions follow):
~~~~~~~~~~~~~BEGIN COPYING~~~~~~~~~~
Here's some code to make your life easier:
Add the Sub call to the Item Not In List Event on your form. For example, if
the
Terms of Payment field on a vendor form is a List Box, add this code to the
event:
ThingNotInList "tTerms", "Terms", "Terms", NewData, Response
Where "tTerms" is the table name that contains the different payment terms
that
are currently on file
"Terms" is the name of the field that contains the description of
the
payment terms
"Terms" is the description of the field that will appear in the
prompt to save / abandon the new data
NewData is the new terms data that the user entered in the list
box
Response is the users decision to add or abandon the new payment
terms
Here is the structure of the Terms Table (tTerms)
TermsID AutoNumber
Terms Text 50
The primary key is TermsID
' Generic subroutine to add an entry to a table.
============================================================================
Sub ThingNotInList(pThingTableName As String, pThingFieldName As String,
pThingDesc As String, pNewThingName As String, pResponse As Integer)
' Arguments:
' pThingTableName => The name of the table into which the new record
will
be inserted
' pThingFieldName => The name of the field in pThingTableName into
which
the new data will be inserted
' pThingDesc => A text description of the data that is being handled:
"Employee Name", "Author", "Date of Birth", etc. This text will appear in
the
message box that prompts the user to add pThingFieldName to pThingTableName
' pNewThingName => The new data that will be inserted into the
pThingFieldName field. It's the info that the user typed into the list box
onthe
form.
' pResponse => The user's decision: Add the data or not:
DATA_ERRADDED
or DATA_ERRCONTINUE
' Bill Nicholson: (e-mail address removed)
Dim status As Integer
Dim mydb As Database
Dim thingRecordset As Recordset
Dim thingField As Field
Dim thingTblDef As TableDef
Dim thingSize As Long
status = MsgBox("The " + pThingDesc + " that you entered is not on file." +
vbNewLine + " Do you want to add it?", 4 + 32, "")
If status = 6 Then ' User clicked "Yes" - he wants to add the Thing
Set mydb = CurrentDb
' Look up the field size
Set thingTblDef = mydb.TableDefs(pThingTableName)
With thingTblDef
For Each thingField In .Fields
If thingField.Name = pThingFieldName Then
thingSize = thingField.Size
Exit For
End If
Next thingField
End With
Set thingRecordset = mydb.OpenRecordset(pThingTableName)
thingRecordset.AddNew
thingRecordset(pThingFieldName) = Mid$(pNewThingName, 1, thingSize)
thingRecordset.Update
thingRecordset.Move 0, thingRecordset.LastModified ' Go to new record
thingRecordset.Close
mydb.Close
pResponse = DATA_ERRADDED ' Access will add the data to the
combo
box
Else
pResponse = DATA_ERRCONTINUE ' User clicked "No" - he does not
want
to add the Thing
End If
End Sub
~~~~~~~~~~~~END COPYING~~~~~~~~~~~
So far, my debugger stopped on:
"thingSize = thingField.Size" as it didn't recognize ".Size" I changed that
to ".ActualSize" as my guess for the closest thing on the available list.
Next, it stopped on:
thingRecordset.Move 0, thingRecordset.LastModified ' Go to new record
It didn't recognize ".LastModified" I couldn't find any likely equivalent
for that. (I was sort of hoping it would just "work" since I'm pretty new at
this.) There is an example of ".LastModified" in the help file, but it
doesn't show up as an option in that drop down list that appears when you
type the "dot" after the word "thingRecordset"
Any help would be greatly appreciated.
Alice