Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Run query in Access VBA and assign result to unbound text box on form

Posted on 2016-11-04
4
66 Views
Last Modified: 2016-11-07
On an Access 2013 form, I need to run a query in a VBA event code and assign the result to an unbound textbox on the form.

Here’s the gist of what I’m doing…

We have a form whose Record Source is a table called CONTACT.  As you might imagine, it displays Name, Title, etc., as well as OrgName.  The event code I mentioned is a button click event that launches a macro that ultimately produces a sheet of address labels for the person whose record is being viewed on the form. All this works great!  Unfortunately, some of the OrgNames are too long to fit on the labels, so we want to pull an abbreviated name (AltOrgName) from a different table called ORGANIZATION, and use that for the label-making feature.

I need a query that will select AltOrgName from ORGANIZATION, where ORGANIZATION.OrgName is the same as Me!OrgName, and then assigns that AltOrgName to the unbound textbox.  The SQL would look like: SELECT ORGANIZATION.AltOrgName from ORGANIZATION where (ORGANIZATION.OrgName = Me!OrgName) -- but I just can’t figure out how to run it properly in the VBA code and assign it to the textbox.

A bit more info:  The VBA event code pulls together Name, Title, etc. from several form fields and assembles this data into a hidden label textbox to base the label on.  Thus, I need for the AltOrgName value to be assigned to its own form field (the unbound textbox, called txtAltOrgName) where the event code can find it, too.  Then, as the event code is pulling data together, it will see if an AltOrgName is present before just using OrgName:

---------------------------
'Check for AltOrgName; will use it for label if there is one
    If IsNull(Me.txtAltOrgName) Then
        If IsNull(Me.OrgName) Then
            var5 = ""
        Else
            var5 = Me.OrgName & vbCrLf
        End If
    Else
        var5 = Me.txtAltOrgName & vbCrLf
    End If
---------------------------

So my question again is, How do I run the query in VBA and how do I set the query result to Me.txtAltOrgName?

Thanks very much for the assistance.
0
Comment
Question by:Galisteo8
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
COACHMAN99 earned 500 total points
ID: 41875015
try
yourtextbox=DLOOKUP("AltOrgName ","ORGANIZATION", "ORGANIZATION.OrgName = '" & Me!OrgName & "'")

i.e. single double space ampersand Me!OrgName ampersand space double single double close-brace
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 41875019
you can use the dlookup() function

dim strOrgName as string
strOrgName=dlookup("AltOrgName","ORGANIZATION","[OrgName]='" &  Me.OrgName & "'")
Me.txtAltOrgName =strOrgName
var5 = Me.txtAltOrgName & vbCrLf
0
 
LVL 7

Expert Comment

by:COACHMAN99
ID: 41875024
that looks very much like what I suggested?

s/b  single double space ampersand space Me!OrgName space ampersand space double single double close-brace
0
 
LVL 8

Author Closing Comment

by:Galisteo8
ID: 41877191
Thanks! I knew I was making it too complicated!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question