View Full Version : Need help with Access VBA & Recordsets


sthomp04
10-25-2004, 05:51 AM
Need help with Access VBA & Recordsets

Have an access DB and am coding some VBA on the back end. When trying to go to a new record I am having some problems.

My main form fTransaction has a series of buttons which allow the user to open a popup form with some extra data(all data is in the same table).

Things seem to go well, and I can edit the data fine until I try to go to a new record.

The popup form will create a new record but then when it closes I want the main form “fTransaction” to be on the same (newly created) record. Here is the trouble. I cant get the form to go to the same newly created record. I am guessing that the main form is not working with the same recordset that the popup form has????

Since this is my first foray into using recordsets I assume my coding and or logic is not correct. I am willing to send the database to you for review.

Please help
Thanks
Steve

Have attached DB

fuzzygeek
10-25-2004, 09:20 AM
No database was attached.

1)--Place a definition such as this one in the top of the form module

Public lngLastFocus As Long


2)--On the “Add Record” Button (for me: Private Sub cmdAddRecord_Click())
Add the following type code. This gets what the current record is.

lngLastFocus = Me.CurrentRecord

3)--On the OnActivate event, use code similar to the following. If the form is activated for the first time, let it operate as normal. Otherwise, you are telling the form to go to the current record. This takes you back to the record you were at before the add.

If lngLastFocus <> 0 Then
DoCmd.GoToRecord , , acGoTo, lngLastFocus
End If

4)--You would have to record the id of the record you added and find in after a requery of the recordset data. Then set lngLastFocus to that record number (equal to the number at the base of the screen. Do the GoToRecord Action and you should be at the record you want on the form. I use this with continuous forms.


Study the GoToRecord action in the vba help screen.

sthomp04
10-25-2004, 12:41 PM
fuzzygeek
Thank you for your help. In studying the code it doesn't look like that solution would fix my problem, could you check my DB (which is now attached) and let me know if this is the best solution for me.