Link to home
Start Free TrialLog in
Avatar of cwbarrett
cwbarrett

asked on

Search fails

Hi,

Not sure if this is the right way to do this but...  When I open certain forms I want to automatically restore to the last record displayed prior to closing the form. I have a table with fields having the "Last" record edited for various forms.   The table will always have 1 record and the fields represent the "Last" record on specific forms displayed before closing the form.  The table is called tblLastRecordNo and a form with a respective query display the 1st (and only) record. This form will be hidden as there is no need for it to be visible.

To make things less complicated, the "Last Record Numbers" form is a subform (not visible) in the form I want to automatically restore to the "Last record displayed".  I use the SearchForRecord macro but it doesn't work.  It doesn't find the record and I have a feeling it has to do with field types.  In the SearchForRecord macro where I enter the Where Condition it looks like this:
[autonumCustomerId] = Forms![frmEquipmentEdit]![frmLastRecordNo1].Form![numCustomerLast]
It doesn't find it.  If I enter:
[autonumCustomerId] = 22
it will find the correct record.  Which leads me to believe there is a number "Type" problem.

[autonumCustomerId]  in my Customer table is AutoNumber.
[numCustomerLast] is a numeric field, Long Integer in tblLastRecordNumber.

I am not a VBA expert so I prefer to use macros when possible.  This seems like such a simple problem but I can't make it work.

Any help is appreciated.

Charlie
Avatar of mbizup
mbizup
Flag of Kazakhstan image

I don't think it is a problem with the data type, but possibly the timing of the macro.

Give this a try in VBA (I know you'd rather have a macro, but just try this) in the form's open Event:

Dim rs as dao.recordset
Set rs = Me.recordsetclone
rs.findfirst "autonumCustomerId = " & Me.frmLastRecordNo1.Form!numCustomerLast
If rs.NoMatch = False then Me.Bookmark = rs.BookMark
Avatar of cwbarrett
cwbarrett

ASKER

Thanks for replying.

I'm getting a "User defined type not defined" error.  Seems to be the dao.recordset is not defined in a module "Type" statement.  What would the Type statement look like for this data type?
ASKER CERTIFIED SOLUTION
Avatar of mbizup
mbizup
Flag of Kazakhstan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Works!  Seems like timing is a problem with Access 2010.  When you think the process flow is correct it sometimes just doesn't work.

Thank you.
Glad to help out -

I imagine that if you used the same macro on a single-form setup (ie: no subform), that you would get the expected results.  When a form opens, events such as loading main form data, creating a subform, loading subform data, etc occur in a specific order that is slipping my old brain at the moment.

One of the benefits of using VBA is that there are specific form-based events (Load, Open, Activate, Current...) that also occur in a specific order, so by choosing the right form event, you can assure that your code runs AFTER the subform has loaded its data.

The timing of a macro is not so clear cut, and in this case I'd hazard a guess that your original macro was firing before the subform actually loaded its data.