Hi Dale:
Thanks for helping, and thanks to BruceM. I'd really be up the creek
without this forum.
By adding an invisible text box to my main form, to hold the value of the
FirmID key, I've now gotten the dialog form to behave in the "sticky"
fashion, so I'm almost there, but have a couple more questions.
I use a public function to open many of my forms. This way, the same bit
of
code performs the same kind of job using different values, plus I can run
the
code directly from an on-click event without having to go thru a private
sub.
This function now has two arguments: the name of the form to open, and the
name of the calling form. The name of the calling form is passed to the
opening form via OpenArgs.
Here is the code for the On Load event of my dialog form, minus error
handling:
Private Sub Form_Load()
'Show last Firm searched.
Dim strArgs As String
strArgs = Me.OpenArgs
Me.lstFirm = Forms(strArgs).txbFirmID
Exit Sub
End Sub
This just makes the value of the list box in the dialog form equal to the
FirmID of the current record in the main form.
Question1: When the main form is first opened, it automatically shows the
first record in the table. How can I change this? When first opened, I
want
the main form to either be blank, or to go to the first firm in
alphabetical
order, not to record no. 1.
Question2: After the "sitcky" dialog form pops up, is there a way to make
it's list box scroll down a little so the chosen value is centered in the
list box, rather than being at the bottom? I want the user to be able to
see
firm names before and after the current firm.
In reply to your question: As BruceM indicated, FindRecord searches the
field that is bound to the control that has focus in the form. It's like
FindRecord assumes that the key field will be present in the form, which
harkens back to the days of user-controlled key values. Using an
invisible
text box doesn't work because it can't be given focus. So FindRecord
works
well with visible keys, but not with invisible, i.e. autonumbering keys.
This lack of utility leads to RecordsetClones and Bookmarks. Access VBA
2007
Programmer's Reference, p. 255, admits: "To reposition the form, you use
a
RecordsetClone. This is a strange concept to developers when they first
use
it."
Dale Fye said:
Although it is not required, I will generally put the Autonumber field in
a
textbox on the form, but will set it's visible property to false
(actually I
have it setup so that when I am using the application in my debugging
mode
it sets the visible property to True, otherwise, it sets the visible
property to False). You could also include the autonumber field in the
forms record source, but not actually bind it to a control. If you do
that,
then you can refer to it as me.[ID] instead of me.txt_ID (assumes you
name
the textbox using a naming convention).
I'm not sure why you state that surrogate keys were an afterthought, or
what
tools you would like to have to deal with them. I'm also not sure what
you
mean about the FindRecord method being useless "under these
circumstances".
Post the SQL you are using for the Load event of your search form. I'll
take a look and see if I can figure out what I did wrong (or whether you
just didn't translate my control and field names to what you have in your
table/form).
Dale
Hi Dale, and thanks for replying.
Yes, the search form is called from main form and then closed when Firm
is
selected. So yes, the main form is aware of the last firm selected.
There is an additional complication: the value of the list box in my
search
form is the firmID number for the selected firm, which is an autonumber
key.
The main form does not contain a control for this field, since the
whole
idea
of a surrogate key is that you don't have to see it or worry about it.
Would I actually have to put the key field on the main form and make it
invisible?! It seems like surrogate keys were an afterthought in
Acces,
and
no tools were added or enhanced to handle them. For instance, I just
learned
that FindRecord, an otherwise simple and powerful method, becomes
useless
under these same circumstances.
Also, I'm getting compile errors. In the "If" statement, my main form
name
is not being recognized; it is flagged as an undefined variable. The
"elseif" statement is also failing because Access expected end of
statement.
I've tried monkeying with the syntax, but have gotten nowhere.
The thought occurred to me to simply make the search form invisible
instead
of closing it, and then make it visible again. There are probably a
host
of
reasons why this won't work. One thing we have to plan for is the
difference
between the first time the search form loads, where it should go to the
top
of the list, and subsequent loads, where it should go to the specified
firm.
--o.b.p.
:
It sounds like you are popping up your search form from a main form,
and
then closing it again when you are done with the search. Is that
accurate?
If so, then the record in your main form should have the information
for
the
company you last selected, shouldn't it?
If so, you could use the search forms load event to set the value of
the
list box to the value from the appropriate field in your main form.
Something like:
Private Sub Form_Load
if currentproject.allforms(MainFormName).isloaded = false then
'don't know how you would get here
msgbox "Main form is not loaded"
elseif len(forms("MainFormName").txtCompanyID & "") = 0 then
me.lstCompanies = NULL
else
me.lstCompanies = forms("MainFormName").txtCompanyID
endif
end sub
HTH
Dale
This has probably been asked before, but I didn't find a thread on
it,
probably due to not knowing how to search.
The user is using a form to do maintenance on Firms table. A list
box
on
a
dialog form is used to choose the desired firm, by name. The list
box
always
appears in alphabetical order, starting at the top of the list. How
can I
bookmark the list box so as to return to the last firm chosen,
rather
than
always going back to the top of the list? I want the list box to be
"sticky"
or to remember where the user last searched.
Thanks