How To Identify The Last Row Worked On (VBA Variable Scope)

So, I had an interesting question regarding how to find the last row the user was working on before the closed the form, so when the reopen the form, they could continue to work on that record.

(This is actually expanding on an example I showed: https://vbahowto.com/how-to-highlight-the-current-row-in-a-continuous-form-in-3-steps/)

The the following code will demonstrate one way you can handle the situation.

Here is all the form code:

Private Sub Form_Unload(Cancel As Integer)
    'MsgBox "Last Record Worked On: " & Me.CustomerID
    Dim strSQL As String
    
    strSQL = "UPDATE MarkedRow SET CustomerID ='" & Me.CustomerID & "' WHERE FromForm ='frmB'"
    CurrentDb.Execute strSQL
    
End Sub


Private Sub Form_Open(Cancel As Integer)
 
    Dim strCriteria As String
    
    strCriteria = Nz(DLookup("CustomerID", "MarkedRow", "FromForm ='frmB'"), 0)
    
    Me.Recordset.FindFirst "CustomerID='" & strCriteria & "'"
    
End Sub

Private Sub Form_Current()
    Me.txtBlank = Me.CustomerID
        
End Sub

Simply, I am using the “Form_Unload” event to store the value of the current form row in a table.

VBA Variable Scope

As you may or may not know, that state of the variables on a form are for the time the the form is open. So I have to store the values I want to keep in a table if I need the program to remember where any variables.

It’s kind of like storing program information for websites in “cookies”.

Then I read these variables from the table when the form opens on the Form_Open event.

Here is the database for an example:

RowHighlightAndMarkRow.accdb

Let me know if you have any questions.




By the way, if you got or are getting value from the VBA information, please give me a tip, thanks!


These posts may help answer your question too...

How can I interact with other Office applications (Excel) using VBA in Access?

Need to write your Access data or query to an Excel file? Here is the how to do it: Most people are familiar with Excel and know how to use it well (enough), and when you start talking about Access, they get scared off, and don’t know what to do anymore. Well, here you are […]

How To Generate A XML File With Access VBA

XML is used to structure your data. You can send a CSV file to another person, but using XML, you can structure your data in a more elegant format, and get very specific about your fields, and what data is contained within your tags (your field names). This is different than a basic CSV file […]

Mouse Click Counter On Access Form

This post will demonstrate how you can count the number of clicks on your button in a certain time frame. It will function like a game . Here is the database form all in one: Option Compare Database Public m_dteStartTime As Date Public m_dteStopTime As Date Private Sub btnClicker_Click() Dim intValue As Integer Dim rst […]

Shared Access Database Management

Here is a handy setup for those who need to manage a shared access database. Either you can create this from scratch as per the example or add the new tables to your existing database. This setup will track: 1. the users currently using the database 2. what time the user and computer name currently […]

Previous Post

How To Find Certain Files In VBA

Next Post

How To Attach Files In An Access Database