Setting RowSource outside the form

G

Gurtz

Hello,

I am having trouble setting the RowSource of a listbox
from outside the form. ie:

Form1 has 'LBox', and in a click event in Form2, the
following code executes:

Forms![Form1]![LBox].RowSource = "Some SQL stuff"
Forms![Form1]![LBox].Requery
DoCmd.Close

thereby closing the form after adjusting a search query.

Even when "Some SQL stuff" is simply a hard-coded,
simplistic query, the listbox is not updating with any
results.

What am I missing, here?

Thanks for your help, guys!
Gurtz
[email = no $]
 
F

Fredg

Gurtz,
Which form are you intending to closing? Form1 or Form2?
The way your code is written both forms are open when
the code runs.
Form2 changes the rowsource of the list box on Form1,
then Form2 closes, leaving Form1 with the updated List box.

It worked for me exactly that way.

However, if you are trying to permanently change the rowsource
of the List box on Form1, (so that it opens with the new data
the next time you open the form) you must do so in Design View.
By code it would be something like this:

' Close Form1 if it is open
DoCmd.Close acForm, "Form1"
' Open Form1 in Design view
DoCmd.OpenForm "Form1", acDesign
' Change the rowsource
Forms![Form1]![LBox].RowSource = "Some SQL stuff"
' Close and Save the change
DoCmd.Close acForm, "Form1",acSaveYes
' Re-open Form1 in Form View
DoCmd.OpenForm "Form1"

No need to requery, as the form does so when it is re-opened.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top