Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2021
    Posts
    3

    Post Code runs, sometimes, other times errors for .MoveFirst due to being at .EOF

    Some preliminary information.
    This application was written by another individual that hasn't been with the company for several years.

    I have dabbled with VBA in Excel, lots of differences and function that I am not familiar with in Access.

    There is an Excel application that is used by many other departments that uses 3 of the 4 Global Constants used for URLs where this data is found.

    Several months ago, the locations were moved to different servers/URLs. Both the Excel application, and this Access application ceased functioning. I reached out to the owner of the Excel app and he sent me an updated copy of the sheet. After poking through the Excel code, I found the paths. I looked at the Access code and found the same information but with an additional URL.

    I updated the Access to match the Excel paths and it looked like things were good. However, the Access application will not always run. I went into the code, rem’d the On Error statement to see where the code was faulting. It seems that a RecordSet is already at .EOF when it encounters a .MoveFirst command. The fact that this application will run at times and have no problem while erroring out other times makes me think it could be a timing issue with the data or network due to a plethora of “Sleep” and “Do Event” commands peppered through the code or another reason that I can't put a finger on.




    With rs
    .MoveFirst
    Do Until .EOF

    In the part of the code below referencing WebBrowser0 is where I think my issue is. I don’t think the data is being picked up.
    Italic text is modified code to not show actual info but I hope I’m explaining things well enough to not be totally confusing.
    This first bit of code is where I think the issue is.

    Location-path is the server page url
    ToolString is the string with all the separate entities concatenated together

    Code:

    With Forms!Main.Form!ToolStatus.Form.WebBrowser0
    .navigate Location-path & ToolString
    While .Busy Or .ReadyState <> READYSTATE_COMPLETE: DoEvents: Wend
    Do While .ReadyState <> 4: DoEvents: Loop
    End With


    It then starts through 4 different With statements that relate to equipment that is grouped by what it is used for.

    If [Forms]![Main].[Form].[cboArea] = "ToolsA" Then
    Sleep (1000)
    With Forms!Main.Form!ToolStatus.Form.WebBrowser1
    .navigate ToolsA1
    Do While .ReadyState <> 4: DoEvents: Loop
    End With
    Sleep (1000)
    With Forms!Main.Form!ToolStatus.Form.WebBrowser2
    .navigate ToolsA2
    Do While .ReadyState <> 4: DoEvents: Loop
    End With
    Sleep (1000)
    With Forms!Main.Form!ToolStatus.Form.WebBrowser3
    .navigate ToolsA3
    Do While .ReadyState <> 4: DoEvents: Loop
    End With
    Sleep (1000)
    With Forms!Main.Form!ToolStatus.Form.WebBrowser4
    .navigate ToolsA4
    Do While .ReadyState <> 4: DoEvents: Loop
    End With

    Else ToolsE



    Set HTMLDoc = Forms!Main.Form!ToolStatus.Form.WebBrowser0.Docume nt

    The next line of code is where things don’t happen.

    Set Tables = HTMLDoc.getElementsByTagName("TABLE")

    I put” If Tables.Length = 0 Then Stop” in as a troubleshooting step and yeah, the code would stop here if this line was not there.


    Thank you in advance for any input.

  2. #2
    Join Date
    Aug 2021
    Posts
    3
    I should add this info
    This .MoveFirst statement is in a For Next loop with the For statement being
    For Column = 0 To C - 1

    At this time I'm not sure how much code I can share. apologies.

  3. #3
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    You should always check for records in a recordset before trying to issue a movefirst or movelast.

    Code:
    If rs.EOF or rs.BOF then 
         MsgBox "No records to process" 
         Exit Sub
    End If
    The Sleep and DoEvents coding appear to all be to with interacting with a web browser, presumably to ensure it has had a chance to get the appropriate information?
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  4. #4
    Join Date
    Aug 2021
    Posts
    3
    Minty,

    I understand that the Sleep and DoEvents are allowing things to be accomplished while data should be collected.

    I also understand that checking for records would be something to do. I've essentially put a "check" in the code.

    I've put the following line of code in "If Tables.Length = 0 Then Stop" after the Set Tables line of code I referrenced in my original post. Too often it comes back as 0. What I'm trying to figure out is why "Tables" does not have records or if there is another area that is preventing the data from being seen.

    My issue is that I am not the original author of this code and still a novice with VBA. Up until a few months ago this code ran without fail until there was a change in the locaton of the data. Again, the Excel application has no issue with what it does and is not as complex. But this Access application isn't always pulling data.

  5. #5
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,928
    What data type/0bject is tables? .length is not a property I recognise

    Set is used for objects such as recordsets, and length implies a string although as stated it is not a property of a string

    Have you got option explicit at the top of each module, disabled error checking and compiled your code?

Please reply to this thread with any new information or opinions.

Similar Threads

  1. What code runs before form Open?
    By vicrauch in forum Forms
    Replies: 4
    Last Post: 05-01-2021, 07:50 AM
  2. Code that runs one time only
    By shod90 in forum Forms
    Replies: 1
    Last Post: 01-03-2017, 05:09 AM
  3. Replies: 2
    Last Post: 08-19-2013, 01:14 PM
  4. Code Runs Perfectly... Every other time.
    By offie in forum Programming
    Replies: 5
    Last Post: 08-14-2013, 03:45 PM
  5. Making sure code runs regardless
    By nvrwrkn in forum Programming
    Replies: 2
    Last Post: 10-09-2012, 11:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums