the endless looping loop (1 Viewer)

fearmichaele

Registered User.
Local time
Today, 02:59
Joined
Jan 14, 2014
Messages
34
I have some VBA code in Access 2013 that i just can't figure out what I am doing wrong. I open two separate queries. I match data from one query with the other by using two fields, name field and a date field. when a match is made the place the date from the first query into the second query, and save the data. i then go to the next record in the first query and repeat the process. my do loop statement works but the problem is, even though i update all of the records in the query, it never exits the do ... loop statement. It goes on and on until I interrupt it with the Esc button.
Here is the excerpt of the do ..... loop that i am using.

Do While Not Me.Recordset.EOF
Me.AllowAdditions = True
Set rec1 = dbs.OpenRecordset(queryname) 'dbOpenDynaset, dbSeeChanges)
Set rec2 = dbs.OpenRecordset(queryname2)
ptname = Me.Patient_First & " " & Me.Patient_Last
disdat = CDate(rec1("Pharmacy Confirmed Rx"))

rec2.Edit
rec2("date dispensed") = disdat
If Left(rec2("Drug Name"), 4) = "Thal" Or "THAL" Or "thal" Then
rec2("Triaged out by Pharmacy") = disdat
Else
End If
rec2.Update
rec2.Close
If Me.Recordset.AbsolutePosition + 1 < Me.Recordset.RecordCount Then
DoCmd.GoToRecord , , acNext

End If
Loop

:banghead:

Any ideas? thanks in advance for your assistance.
 

Ranman256

Well-known member
Local time
Today, 03:59
Joined
Apr 9, 2015
Messages
4,337
an update query will do this without any code.
 

jdraw

Super Moderator
Staff member
Local time
Today, 03:59
Joined
Jan 23, 2006
Messages
15,379
I agree with ranman --Update query

Sometimes, it is better to post a brief description of what you are trying to accomplish and ask readers for ideas/options.

Good luck with your project.
 

fearmichaele

Registered User.
Local time
Today, 02:59
Joined
Jan 14, 2014
Messages
34
Thanks for the replies, this is what I am trying to attempt to do. I am writing a VBA code that will run as a daily autoexec, which will perform the following function.
1. Open a website
2. Log me into that website using my log in credentials
3. Navigate through the multiple pages of the website where I can download a report that I need in excel format
4. Save the excel file and import it into my database by linking the excel file.
5. Close out the website.
All this is done and works great.
The next steps are to match the data in the linked excel table to a query in my database.
What my query does is show only people who do not have a date in a specific field. This date will come from the excel table if the person is on that table.
So currently my code is running off the query with the people that do not have the date. (ME. recordset)
I am opening the second query that is linked to the excel table to find the data I want after matching the person’s name and a second identifier. I then grab the date from the excel field and place it into the correct field in my query.
I had to use a do while …. Loop because I couldn’t get it to go past the first record in my first query.
The loop works, updating all the appropriate records with the correct date but it does not exit the loop. It continues to run until I press the Esc. Button. It does not recognize the end of file.
 

jdraw

Super Moderator
Staff member
Local time
Today, 03:59
Joined
Jan 23, 2006
Messages
15,379
Can you show us your database? I'm interested in getting to the website from Access/vba.

This one confuses me...
Save the excel file and import it into my database by linking the excel file.

are you importing or linking? My guess is importing. If you have imported and saved the data, what exactly is the issue?
 

fearmichaele

Registered User.
Local time
Today, 02:59
Joined
Jan 14, 2014
Messages
34
it is a linked table to the excel spreadsheet i downloaded to a folder on a virtual server provided by my work.
 

fearmichaele

Registered User.
Local time
Today, 02:59
Joined
Jan 14, 2014
Messages
34
Can you show us your database? I'm interested in getting to the website from Access/vba.

This one confuses me...


are you importing or linking? My guess is importing. If you have imported and saved the data, what exactly is the issue?

it is a linked excel table. the code below is what i use to open the webpage, navigate to the page and download the file. after download i have to open the file and get rid of headers in the spreadsheet that i dont need. then save the file.

Private Sub Command19_Click()
'routine to open webpage, download dispense data, import it into dashboard.

'log in information
Const strURL = enter the full website url here in quotation marks
Const strID = this is where my user name goes
Const strPswd = this is my passoword
strexcelpath = this is the path where i want to save the excel spreadsheet
todaydt = Date ' todays date
lastweek = Date - 7 ' go back 7 days

' import from website
' please note that the twait is important to set correctly or you can end up on a different page of your website

Set objie = CreateObject("InternetExplorer.Application")
objie.Visible = True
objie.Navigate2 strURL 'open website
Set objshell = CreateObject("WScript.Shell")
TWait = Time
TWait = DateAdd("s", 3, TWait)
Do Until TNow >= TWait
TNow = Time
Loop
'log in script
objshell.SendKeys strID ' types in my logon for me
TWait = Time
TWait = DateAdd("s", 0.5, TWait)
Do Until TNow >= TWait
TNow = Time
Loop
objshell.SendKeys "{TAB}" ' tabs to the password field
Wait = Time
TWait = DateAdd("s", 0.5, TWait)
Do Until TNow >= TWait
TNow = Time
Loop
objshell.SendKeys strPswd 'types in my password field for me
TWait = Time
TWait = DateAdd("s", 0.5, TWait)
Do Until TNow >= TWait
TNow = Time
Loop
objshell.SendKeys "{ENTER}"
TWait = Time
TWait = DateAdd("s", 3, TWait)
Do Until TNow >= TWait
TNow = Time
Loop
'navigates to the correct page. i had to count how many tab key strokes it took to get to the right button on the webpage

objshell.SendKeys "{TAB 19}"
TWait = Time
TWait = DateAdd("s", 1, TWait)
Do Until TNow >= TWait
TNow = Time
Loop
objshell.SendKeys "{ENTER}"
TWait = Time
TWait = DateAdd("s", 5, TWait)
Do Until TNow >= TWait
TNow = Time
Loop
objshell.SendKeys "{TAB 14}"
TWait = Time
TWait = DateAdd("s", 1, TWait)
Do Until TNow >= TWait
TNow = Time
Loop
objshell.SendKeys "{ENTER}"
TWait = Time
TWait = DateAdd("s", 5, TWait)
Do Until TNow >= TWait
TNow = Time
Loop
objshell.SendKeys "{TAB 15}"
TWait = Time
TWait = DateAdd("s", 1, TWait)
Do Until TNow >= TWait
TNow = Time
Loop
objshell.SendKeys "{ENTER}"
TWait = Time
TWait = DateAdd("s", 3, TWait)
Do Until TNow >= TWait
TNow = Time
Loop
objshell.SendKeys "{TAB 15}"
TWait = Time
TWait = DateAdd("s", 1, TWait)
Do Until TNow >= TWait
TNow = Time
Loop
objshell.SendKeys lastweek ' enter the date range for the report this is the start date
TWait = Time
TWait = DateAdd("s", 0.1, TWait)
Do Until TNow >= TWait
TNow = Time
Loop
objshell.SendKeys "{TAB}"
TWait = Time
TWait = DateAdd("s", 0.5, TWait)
Do Until TNow >= TWait
TNow = Time
Loop
objshell.SendKeys "{DOWN}"
TWait = Time
TWait = DateAdd("s", 0.1, TWait)
Do Until TNow >= TWait
TNow = Time
Loop
objshell.SendKeys "{TAB}"
TWait = Time
TWait = DateAdd("s", 0.5, TWait)
Do Until TNow >= TWait
TNow = Time
Loop
objshell.SendKeys todaydt ' this is the end date
TWait = Time
TWait = DateAdd("s", 0.1, TWait)
Do Until TNow >= TWait
TNow = Time
Loop
objshell.SendKeys "{TAB}"
TWait = Time
TWait = DateAdd("s", 0.5, TWait)
Do Until TNow >= TWait
TNow = Time
Loop
objshell.SendKeys "{ }"
TWait = Time
TWait = DateAdd("s", 0.5, TWait)
Do Until TNow >= TWait
TNow = Time
Loop
objshell.SendKeys "{TAB}"
TWait = Time
TWait = DateAdd("s", 0.5, TWait)
Do Until TNow >= TWait
TNow = Time
Loop
objshell.SendKeys "{ }"
TWait = Time
TWait = DateAdd("s", 0.5, TWait)
Do Until TNow >= TWait
TNow = Time
Loop
objshell.SendKeys "{TAB}"
TWait = Time
TWait = DateAdd("s", 0.5, TWait)
Do Until TNow >= TWait
TNow = Time
Loop
objshell.SendKeys "{ }"
TWait = Time
TWait = DateAdd("s", 0.5, TWait)
Do Until TNow >= TWait
TNow = Time
Loop
objshell.SendKeys "{TAB}"
TWait = Time
TWait = DateAdd("s", 0.5, TWait)
Do Until TNow >= TWait
TNow = Time
Loop
objshell.SendKeys "{ }"
TWait = Time
TWait = DateAdd("s", 0.5, TWait)
Do Until TNow >= TWait
TNow = Time
Loop
objshell.SendKeys "{TAB 16}"
TWait = Time
TWait = DateAdd("s", 1, TWait)
Do Until TNow >= TWait
TNow = Time
Loop
objshell.SendKeys "{ENTER}"
TWait = Time
TWait = DateAdd("s", 5, TWait)
Do Until TNow >= TWait
TNow = Time
Loop
objshell.SendKeys "{TAB 39}"
TWait = Time
TWait = DateAdd("s", 5, TWait)
Do Until TNow >= TWait
TNow = Time
Loop
objshell.SendKeys "{ENTER}"
TWait = Time
TWait = DateAdd("s", 1, TWait)
Do Until TNow >= TWait
TNow = Time
Loop
objshell.SendKeys "{down 2}"
TWait = Time
TWait = DateAdd("s", 1, TWait)
Do Until TNow >= TWait
TNow = Time
Loop
objshell.SendKeys "{ENTER}"
TWait = Time
TWait = DateAdd("s", 1, TWait)
Do Until TNow >= TWait
TNow = Time
Loop
objshell.SendKeys strexcelpath ' tells where to save the downloaded excel file
TWait = Time
TWait = DateAdd("s", 1, TWait)
Do Until TNow >= TWait
TNow = Time
Loop
objshell.SendKeys "{ENTER}"
TWait = Time
TWait = DateAdd("s", 3, TWait)
Do Until TNow >= TWait
TNow = Time
Loop

objshell.SendKeys "{y}"
TWait = Time
TWait = DateAdd("s", 1, TWait)
Do Until TNow >= TWait
TNow = Time
Loop

objie.Quit ' close the webpage
Set objchell = Nothing
Set objie = Nothing

' open imported excel spreadsheet and clean up rows
TWait = Time
TWait = DateAdd("s", 1, TWait)
Do Until TNow >= TWait
TNow = Time
Loop
Dim xl As Object

'Step 1: Start Excel, then open the target workbook.
Set xl = CreateObject("Excel.Application")

xl.Workbooks.Open ("your excel file")

'Step 2: Make Excel visible
xl.Visible = True

'Step 3: delete the first 7 rows from the workbook

xl.Rows("1:7").Delete

'Step 4: Close and save the workbook, then close Excel
xl.ActiveWorkbook.Close (True)
xl.Quit

'Step 5: Memory Clean up.
Set xl = Nothing

from here it goes into my code which i have the questions. my loop does not end when eof is reached..
 

GohDiamond

"Access- Imagineer that!"
Local time
Today, 03:59
Joined
Nov 1, 2006
Messages
550
You are confusing Recordset commands. It goes like this

While Not rs1.EOF 'Start from the first record in rs1 thru the END of rs1

'Do something here while on the current record of rs1

rs1.MoveNext 'go to next record in rs1

Wend 'Exit the loop when you get to the end of rs1

Cheers!
Goh
 

fearmichaele

Registered User.
Local time
Today, 02:59
Joined
Jan 14, 2014
Messages
34
AWESOME!!!!!!:D:D:D

thank you. the while wend worked like a charm.

many thanks
 

Users who are viewing this thread

Top Bottom