Who can help me!

Jesmine

Registered User.
Local time
Today, 16:09
Joined
Sep 7, 2004
Messages
10

The problem is that I have a table called student, and in it there is a city field...
in the student form I want to get the student's city from a combo box which gets it's information from the city table....
now if the student is from a city doesn't already exists in the city table... I want to add the new city to the city table when I add the student, so that I'll not have to add the city to city table by myself....
for this task I tried this code...



Private Sub cmb_City_LostFocus()
Dim found As Boolean
found = False
Dim RST As Recordset
Set RST = CurrentDb.OpenRecordset("City_T", dbOpenTable)
RST.MoveFirst
Do Until RST.EOF
If RST![City] = Me.cmb_City.Text Then
found = found Or True
Else
found = found Or True
End If
RST.MoveNext
Loop
RST.MoveLast
If Not found Then
RST.AddNew
RST![City] = Me.cmb_City.Text
RST.Update
End If
RST.Close
End Sub

and yet it's not working,
so I was wondering what is the problem!!!! :confused:

so can you please tell me how can I fix that!!!!

thx for your time....
 
Use the NotInList event, there have been many examples posted here
 
Not sure why...

Jesmine:

you have this code running on Lost Focus.

Why not just have the form's combo box based on a query that selects all cities from your student/city table, selecting unique values only. Make sure the record source is set to the city field.

When a user enters a new student record, and tries to enter the city the combo box will allow selection of the correct ciry. If the city does not exist in the table, it will then be manually added to the table via the combo box.

You may need to refresh your recordset after update of the form to make sure that you will be able to select newly added cities, and you also want to make sure that the form property 'Limit to List' is set to No.

:)
 
Course if you want say fifteen different spellings of for example New York then the previous posters suggestion is fine, if however you only want one in your table then set the Limit to List to yes and use the NotInList to verify entries
 
Even better...

Jesmine:

Rich is correct, I forgot to mention that another benefit of this method is to to help scrub data. If, for example, you have multiple spellings of any significant data for the same item/object this will point that out so that you will not generate separate report entries for

New York City
New Yorke City
Nu York City

In other words, with proper database design, this should not occur to begin with, but if it does, this will help sort that out for you.

Thanks, Rich! :rolleyes:
 
thx

Thaks Rich and sfreeman@co.mer (Sam_F) for you help

Rich please is it possible to tell me where I can find those examples you told me about them?

thank you both once again
 
Well,
when I wrote this code


Dim strSQL As String
If MsgBox("Are you sure that you want to add this new Record?", vbYesNo) Then
DoCmd.RunSQL ("INSERT INTO City_T VALUES('" & Me.cmb_City.Text & "')")
End If
cmb_City.RowSource = "SELECT City_T.City FROM City_T;"



in the NotInList event nothing happened...
so I tried it in the loseFocas event to know if it's working, so it added the city even if it's already in the city table
so I was wondering how can I get it working in the NotInList Event!!!! :confused:

thx...
 
Hello all!

thx Col for the link ;) ...

I'm not that skilled with VBA, but a friend of mine who knows nothing about VBA asked me for a favor and I really wanted to help her...

But I can figure out what's the problem with what I have done so far!!!
When I'm Adding a new city the function in the NotInList event is working correctly and after the city is inserted into the city table I have a message telling me "select an item from the list, or enter text that matches one of the listed items."!... And when I look at the combo box items I find the new city already listed!!!
So is there anybody like to tell me how to get rid of that message!!!! Because I have No idea how to do that...
Please take a look at my DB...

Thx!
 

Attachments

:(

I still can't figure out what is the problem with my code....
so is there anybody can help me please!!!! :(
 

Users who are viewing this thread

Back
Top Bottom