How to resume a record set where a macro left off?

MCTampa

Board Regular
Joined
Apr 14, 2016
Messages
97
I have a VBA script that loops through our AS400 and changesfields based on a set of criteria loaded into a form.
There are certain times when our AS400 glitches and presentsa half screen of information. At thispoint, I want to back out of the screen and restart the process.However, if I have 5 records loaded in myform, it starts from the beginning.
I would like to have it resume the recordset where it leftoff, but I do not know how to write that.

Edit: I don’t knowwhy the website is fighting with me, but I can’t get my code in one box. Sorry.
I want to simply have the code resume the current rst atafter the “COM”, “PF2” command at the end.


Thanks

Here is my code:
Code:
Function StopSale()
Dim MySession As New AutSess
Dim SessionName As String
Dim FirstEntry, LastEntry As Integer
SessionName = "A"
Code:
[FONT=Calibri][SIZE=3][COLOR=#000000]MySession.SetConnectionByName (SessionName)
Set OIA = MySession.autECLOIA
Set PS = MySession.autECLPS[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]OIA.WaitForAppAvailable
OIA.WaitForInputReady[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Dim cmd1 As ADODB.Command
Set cmd1 = New ADODB.Command
cmd1.ActiveConnection = CurrentProject.Connection[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Dim rst2 As ADODB.Recordset
Set rst2 = New ADODB.Recordset[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]With rst[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]rst.Open "select Entry,Resort_Code,AS400SD,End_Date,Unit_Size,Flag,Return,Track_Code,Res_Line,Unit_Num from Log ", CurrentProject.Connection, adOpenStatic, adLockOptimistic
Do Until rst.EOF = True[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Call sendToNa("PUT", "400", 21, 18, 0, 0) 'Go to Option 400
Call sendToNa("COM", "enter", 0, 0, 0, 0)
Call sendToNa("PUT", .Fields("Resort_Code"), 6, 43, 0, 0) 'Pass Resort ID
Call sendToNa("PUT", .Fields("AS400SD"), 8, 43, 0, 0) 'Pass Start Date
If .Fields("Track_Code") = "ALL" Then Call sendToNa("PUT", "*", 10, 43, 0, 0):  Call sendToNa("COM", "enter", 0, 0, 0, 0) _
Else: Call sendToNa("COM", "PF4", 0, 0, 0, 0) 'Prompt for Track Code selection
If .Fields("Track_Code") = "C" Then Call sendToNa("PUT", "X", 16, 37, 0, 0) _
Else: If .Fields("Track_Code") = "T" Then Call sendToNa("COM", "Roll Up", 0, 0, 0, 0): Call sendToNa("COM", "Roll Up", 0, 0, 0, 0): Call sendToNa("PUT", "X", 18, 37, 0, 0)
 Call sendToNa("COM", "enter", 0, 0, 0, 0)
 Call sendToNa("COM", "enter", 0, 0, 0, 0)[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]
If .Fields("Unit_Size") = "9" And .Fields("Return") = "U" Then
'This macro returns all unit sizes[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Do
If PS.GetText(14, 68, 1) = " " And DateValue(PS.GetText(7, 51, 2) & " - " & PS.GetText(7, 53, 3) & " - " & PS.GetText(7, 56, 2)) <= .Fields("End_Date") Then Call sendToNa("COM", "PF2", 0, 0, 0, 0): Call sendToNa("COM", "PF2", 0, 0, 0, 0): rst.Requery[/COLOR][/SIZE][/FONT]


 
Last edited:
Note: There are two MoveNext commands here. Not good usually.
Code:
rst.Fields("Status").value = "Done"
rst.Update
rst.MoveNext
At the beginning I added

Code:
If rst.Fields("Status").Value = "Done" Then rst.MoveNext
If rst.EOF = True Then ExtFunction

Just one is more common:
Code:
Do While not rst.EOF

    If not rst.Fields("Status").value = "Done" then

        rst.Edit
        '//Updates
        rst.Update
    
    End If

    '//Now move next and repeat
    rst.MoveNext

Loop

Also agree that in general it is also a good idea to just exclude the "done" records from the recordset entirely (in your selection criteria) if you can swing that.
 
Last edited:
Upvote 0

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top