Access 2007 SQL txt field Question

I'm trying to understand how to extract data from a query using SQL to form.

Question: Can you assign a query value to a text field?  or does it always have to be a list box?

Me.txtCity.RowSource = vcatch  returns a value

Me.txtCity2.Value = vcatch returns the SQL statement.


Private Sub Command5_Click()
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String
Dim vcatch As String
Dim vsql As String


If IsNull(Me.txtsearch) = True Then
    MsgBox "Please entered a client ID", vbOKOnly
Exit Sub
Else

vsql = Me.txtsearch

strSQL = "SELECT Table1.City " & vbCrLf & _
"FROM Table1 " & vbCrLf & _
"GROUP BY Table1.CompnayID, Table1.City " & vbCrLf & _
"HAVING (((Table1.CompnayID)='" & vsql & "'));"




vcatch = strSQL

Me.txtCity.RowSource = vcatch
Me.txtCity2.Value = vcatch
Set db = CurrentDb
Set rst = db.OpenRecordset(strSQL, dbOpenDynaset)

If Not (rst.EOF And rst.BOF) Then
      'display your records
              '........................
Else ' if no records found
      MsgBox "No records found!", vbOKOnly

    Set rst = Nothing
    Set db = Nothing
    
End If
End If
End Sub

Open in new window

jbakestullAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
"Can you assign a query value to a text field? "
Not exactly.  A text box does not have a Row Source.

You could ... do this:

=DLookup("[YourFieldName]", "YourQueryName", "<OptionalCriteria>")

including the equals sign .. aka Calculated Control ... that would go in the Text box Control Source property.

mx
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
The code you posted won't even compile because of

Me.txtCity.RowSource = vcatch

assuming txtCity IS really a Text box ...?

mx
0
jbakestullAuthor Commented:
txtcity is a list box

txtcity2 is a test box

I switched the text box type not the naming. Sorry for the confusion.

Could you provide a example on how to use

=DLookup("[YourFieldName]", "YourQueryName", "<OptionalCriteria>")?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
ok.. What value/field ... exactly are you trying to assign to the text box ?

mx
0
jbakestullAuthor Commented:
City value based value entered into txtsearch, which contains the company ID value from table1

0
jbakestullAuthor Commented:
query is named query1
0
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
ok ... so, you question was "Can you assign a query value to a text field?"

So, what is the name of the Field in query1 that you want to assign to the text box ?

mx
0
jbakestullAuthor Commented:
City
0
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:


=DLookup("[City]", "Query1")

Now, this assumes that query1 is only returning one record.  If not, then you will need to apply some criteria ...

mx
0
jbakestullAuthor Commented:
The query does return one record.

So if the query has multiple fields, your able to choose the field you want using "<OptionalCriteria>"?
0
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
"So if the query has multiple fields, your able to choose the field you want using "<OptionalCriteria>"?"
Ummm ... no.  You specify the field in the first argument of the DLookup().  If you only have one record (good) .... then no need for criteria argument ...

mx
0
jbakestullAuthor Commented:
Thanks for your help
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.