SOLVED

Assign information from Query to a Form Field

%3CLINGO-SUB%20id%3D%22lingo-sub-2574823%22%20slang%3D%22en-US%22%3EAssign%20information%20from%20Query%20to%20a%20Form%20Field%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2574823%22%20slang%3D%22en-US%22%3E%3CP%3EHi%3C%2FP%3E%3CP%3EI%20am%20having%20an%20issue%20with%20trying%20to%20populate%20information%20from%20a%20query%20with%20a%20single%20line%20into%20a%20field%20in%20a%20form.%3C%2FP%3E%3CP%3ECan%20someone%20let%20me%20know%20what%20I%20am%20doing%20wrong%20with%20my%20Code%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPrivate%20Sub%20Form_Load()%3CBR%20%2F%3Estrsql%20%3D%20%22SELECT%20SelectionID%2C%20SalesAdministrator%2C%20Customer%2C%20Week%2C%20Commodity%20FROM%20FilterCriteria%20WHERE%20(SelectionID)%3D1%20%3B%22%3CBR%20%2F%3Ex%20%3D%20InputBox(%22%22%2C%20%22%22%2C%20%22'%20%26amp%3B%20strsql.Customer%20%26amp%3B%20'%22)%3CBR%20%2F%3EMe.Combo58.Value%20%3D%20%22'%20%26amp%3B%20strsql.Customer%20%26amp%3B%20'%22%3CBR%20%2F%3EMe.WeekCombo.Value%20%3D%20%22'%20%26amp%3B%20strsql.Week%20%26amp%3B%20'%22%3CBR%20%2F%3EMe.CommodityCombo.Value%20%3D%20%22'%20%26amp%3B%20strsql.Commodity%20%26amp%3B%20'%22%3CBR%20%2F%3EDoCmd.Requery%3CBR%20%2F%3EEnd%20Sub%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIt%20is%20to%20populate%20these%20fields%20which%20are%20blank%20at%20the%20top.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22ajcbutler1120_1-1626951545364.png%22%20style%3D%22width%3A%20539px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F297621i5AD0938CF914C9F1%2Fimage-dimensions%2F539x260%3Fv%3Dv2%22%20width%3D%22539%22%20height%3D%22260%22%20role%3D%22button%22%20title%3D%22ajcbutler1120_1-1626951545364.png%22%20alt%3D%22ajcbutler1120_1-1626951545364.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3EYou%20can%20see%20it%20is%20not%20putting%20the%20value%20of%20the%20query%20field%20in%20the%20first%20field.%3C%2FP%3E%3CP%3EFirst%20field%20should%20be%20Text%2C%20Second%20field%20is%20a%20number%20and%20third%20field%20is%20text.%3C%2FP%3E%3CP%3EI%20would%20really%20appreciate%20some%20help.%3C%2FP%3E%3CP%3EThanks%3C%2FP%3E%3CP%3EAaron%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2574823%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EForm%20field%20update%20vba%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2575092%22%20slang%3D%22en-US%22%3ERe%3A%20Assign%20information%20from%20Query%20to%20a%20Form%20Field%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2575092%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1065743%22%20target%3D%22_blank%22%3E%40ajcbutler1120%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIt%20looks%20to%20me%20like%20you%20are%20assigning%20the%20SQL%20String%20created%20in%20the%20first%20line%20of%20code%20as%20a%20VALUE%20in%20the%20combo%20box%2C%20where%20you%20actually%20want%20the%20RESULT%20from%20the%20execution%20of%20the%20SQL%20String.%20You%20can%20see%20that%20in%20the%20screenshot%2C%20which%20shows%20the%20%26amp%3B%20and%20other%20parts%20of%20that%20string.%3C%2FP%3E%3CP%3EYou%20are%20also%20assigning%20a%20string%20to%20the%20variable%20X%2C%20but%20then%20never%20doing%20anything%20with%20X.%3C%2FP%3E%3CP%3EAlso%2C%20I%20see%20that%20you%20have%20not%20Dimmed%20you%20variables%2C%20strsql%20and%20X.%20Good%20programming%20practice%20is%20to%20ALWAYS%20declare%20variables%20to%20avoid%20problems%20arising%20from%20their%20misuse.%20Unfortunately%2C%20that%20wouldn't%20help%20much%20here%20where%20%22X%22%20is%20never%20used%20for%20anything.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETo%20resolve%20this%2C%20there%20are%20two%20possible%20routes.%3C%2FP%3E%3CP%3EOne%20is%20to%20use%20that%20sqlstring%2C%20or%20perhaps%20another%20one%20more%20appropriate%20to%20the%20situation%2C%20as%20the%20ROWSOURCE%20for%20a%20combo%20box.%20Then%20your%20other%20fields%20Combo58%2C%20etc.%20can%20be%20populated%20by%20referring%20to%20the%20pertinent%20column%20in%20that%20combo%20box.%26nbsp%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CP%3EThe%20other%20is%20to%20open%20a%20recordset%20using%20that%20SQL%20string.%20Then%20you%20could%20refer%20to%20the%20fields%20in%20that%20recordset%20where%20you%20want%20to%20assign%20their%20values%20to%20the%20other%20controls.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'd%20use%20the%20former%20approach%2C%20but%20a%20recordset%20is%20totally%20viable.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2575093%22%20slang%3D%22en-US%22%3ERe%3A%20Assign%20information%20from%20Query%20to%20a%20Form%20Field%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2575093%22%20slang%3D%22en-US%22%3EI'm%20not%20even%20sure%20what%20happens%20with%20that%20Input%20Box%2C%20to%20be%20frank.%20What%20does%20it%20actually%20do%20when%20you%20try%20to%20use%20it%3F%3C%2FLINGO-BODY%3E
New Contributor

Hi

I am having an issue with trying to populate information from a query with a single line into a field in a form.

Can someone let me know what I am doing wrong with my Code?

 

Private Sub Form_Load()
strsql = "SELECT SelectionID, SalesAdministrator, Customer, Week, Commodity FROM FilterCriteria WHERE (SelectionID)=1 ;"
x = InputBox("", "", "' & strsql.Customer & '")
Me.Combo58.Value = "' & strsql.Customer & '"
Me.WeekCombo.Value = "' & strsql.Week & '"
Me.CommodityCombo.Value = "' & strsql.Commodity & '"
DoCmd.Requery
End Sub

 

It is to populate these fields which are blank at the top.

 

ajcbutler1120_1-1626951545364.png

You can see it is not putting the value of the query field in the first field.

First field should be Text, Second field is a number and third field is text.

I would really appreciate some help.

Thanks

Aaron

 

 

6 Replies

@ajcbutler1120 

It looks to me like you are assigning the SQL String created in the first line of code as a VALUE in the combo box, where you actually want the RESULT from the execution of the SQL String. You can see that in the screenshot, which shows the & and other parts of that string.

You are also assigning a string to the variable X, but then never doing anything with X.

Also, I see that you have not Dimmed you variables, strsql and X. Good programming practice is to ALWAYS declare variables to avoid problems arising from their misuse. Unfortunately, that wouldn't help much here where "X" is never used for anything.

 

To resolve this, there are two possible routes.

One is to use that sqlstring, or perhaps another one more appropriate to the situation, as the ROWSOURCE for a combo box. Then your other fields Combo58, etc. can be populated by referring to the pertinent column in that combo box. 

The other is to open a recordset using that SQL string. Then you could refer to the fields in that recordset where you want to assign their values to the other controls.

 

I'd use the former approach, but a recordset is totally viable.

 

I'm not even sure what happens with that Input Box, to be frank. What does it actually do when you try to use it?
Thanks.

The x = inputbox is there just to show me what is being picked up in the strsql so I can visually see it for testing to make sure the right thing is being picked. It is not relevant to what I am trying to do. I can comment that out.

With regards to the combo box row source, it already has one in there that I use to select the customer for example.

How would I open a recordset? I have not done this before.
best response confirmed by ajcbutler1120 (New Contributor)
Solution

@ajcbutler1120 

 

Thanks for clarifying the context. Sometimes more information is better than less in trying understand a problem ;)

So, if your existing combo box already has a row source that returns the customer information, you can use that in the first suggested approach, I think. Add the required fields as additional columns to the rowsource for that combo box. Then, set the control sources for the other controls as, for example,

=cboCustomer.Column(1),

=cboCustomer.Column(2)

and so on.

 

where Column(1) is the second column, left to right. Combo and listboxes are indexed starting with 0 so column(0) is the first one on the left, and the one normally bound to the control. Other columns simply return relevant additional fields, as in this case.

That's probably the least complicated approach, assuming that you can add those fields to the rowsource for that combo box.

 

The Recordset approach would also work, and may be worthwhile if you want to learn a new technique in the process.

 

It might look something like this:

 

Private Sub Form_Load()

Dim strsql as String

Dim db as DAO.Database

Dim rst as DAO.Recordset

Set db = CurrentDB

 

    strsql = "SELECT SelectionID, SalesAdministrator, Customer, Week, Commodity FROM FilterCriteria

         WHERE (SelectionID)=1 ;"


    x = InputBox("", "", "' & strsql.Customer & '") ' Validation only, not used in procedure

 

    Set rst = db.OpenRecordSet(strsql, dbOpenSnapShot)

    With rst

        Me.Combo58.Value = !Customer

        Me.WeekCombo.Value = !Week

        Me.CommodityCombo.Value = !Commodity

    End With

    Set rst = Nothing
    DoCmd.Requery
End Sub

Thats Amazing... Thanks so much for your help George. It works.
Congratulations on solving the problem.
Continued success with the project.