PDA

View Full Version : Solved: Primary key retreival



cath_hopes
10-11-2007, 05:23 AM
There must be a easy solution to this one so hope someone can help...

I want to open a second form at a particular address record that has been selected from a first form.
The first form offers three linked drop down combo lists, the first one for the House name/ number, the second for Address Line 1 and the third for date record created.
These are 3 fields from a table called Property whose primary key is [Property ID].
I need to retrieve the Property Id value in order that the following code should open my second form:
DoCmd.OpenForm "Property Form", acNormal, , , acFormEdit, , "Property ID"

I have tried the following sub but it errors on the RunSQL command:
Private Sub GetPropertyID()
'Create string variable (storage place)named mySQL
Dim mySQL As String
'Add lengthy SQL statement to mySQL in chunks.
mySQL = "SELECT [Property].[Property ID] FROM Property"
'Leading spaces below to ensure spaces between words.
mySQL = mySQL & " WHERE [House Name/ Number]=[Combo16] AND"
mySQL = mySQL & " [Address Line 1]=[Combo18] AND [Date record created]=[Combo20]"
'Hide warning messages provided by action queries:
DoCmd.SetWarnings False
DoCmd.RunSQL mySQL
'Get warning messages back to normal:
DoCmd.SetWarnings True
End Sub

OBP
10-11-2007, 09:50 AM
Why have 3 combos, why not just one combo with all four fields on it on the form. Then use the Property Id, which will be one of the Columns in the Combo box?

cath_hopes
10-11-2007, 11:52 AM
I tried that first of all. The three columns of fields show nicely when you click on the drop down arrow, however when you select one of the rows only the first field is returned. How do I get all columns to show so its more user friendly? (Property ID is the fourth hidden field/ column as you suggest). By the way, I used the wizard to set up this combo.
Thanks,
Catherine

cath_hopes
10-12-2007, 11:45 PM
I have just discovered why my SQL doesn't work - The DoCmd.RunSQL command does not work for non-action queries. I was trying to use it to run a "select" query, which is a non-action query. ("Action"
queries are "append", "delete", “make-table", etc. type of queries.)

I will re-submit this thread as my issue has now changed.
Thanks for reading!!

Catherine