Inputting data into a webpage form automatically (1 Viewer)

stu_c

Registered User.
Local time
Today, 20:17
Joined
Sep 20, 2007
Messages
489
Hi all
I have a webpage form I need filling out once a button is pressed on the Access form, this then auto populates the information from the access form onto the webpage, at the minute I am using tab stops which is starting to get a little messy as I need to press multiple tabs to get to the correct field, is there a way I can do this using the Field ID names from the website?

Code:
Private Sub Command1764_Click()
Dim strLink As String
    Dim blnOpening As Boolean
    Dim strWebTitle As String
    On Error GoTo err_handler
    
    strLink = "http://EXAMPLE-WEBSITE.co.uk"
    strLink = "C:\Program Files\Internet Explorer\IEXPLORE.EXE " & strLink
 
    Call Shell(strLink, vbMaximizedFocus)
    
    blnOpening = True
    Do While blnOpening = True
    blnOpening = False
    
Loop
    Dim started As Single: started = Timer
    Do: DoEvents: Loop Until Timer - started >= 1
  
DoEvents
    Call fSendKeys("{Tab}", True)
    Call fSendKeys("{Tab}", True)
    Call fSendKeys("{Tab}", True)
    Call fSendKeys("{Tab}", True)
    Call fSendKeys("{Tab}", True)
    Call fSendKeys("{Tab}", True)
    Call fSendKeys("{Tab}", True)
    Call fSendKeys("{Tab}", True)
    Call fSendKeys(Me.ReferenceNumber, True)
    Call fSendKeys("{Tab}", True)
    Call fSendKeys("{Tab}", True)
    Call fSendKeys("Me.OrderDate", True)
    Call fSendKeys("{Tab}", True)
    Call fSendKeys("{Tab}", True)
    Call fSendKeys("Me.Address", True)
Exit Sub
    
err_handler:
    If Err.Number = 5 Then 'Invalid procedure call or argument
        blnOpening = True

End If
Resume Next

End Sub
 

stu_c

Registered User.
Local time
Today, 20:17
Joined
Sep 20, 2007
Messages
489

Gasman

Enthusiastic Amateur
Local time
Today, 20:17
Joined
Sep 21, 2011
Messages
14,047
Thanks Gasman for the reply, I have the ID field names from the website they are
IDorderRef
IDOrderDate
IDHomeAddress
etc
Then that link should be great for you?
 

stu_c

Registered User.
Local time
Today, 20:17
Joined
Sep 20, 2007
Messages
489
many thanks
I tried the below codes with no success :(, the webpage opens but does not populate the IDorder field on the webpage

Code:
Doc.getElementById("IDorderRef").Value = LblOrderRef


Then that link should be great for you?
 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 20:17
Joined
Sep 21, 2011
Messages
14,047
Well I think you need the supporting code that went with it as well?

Not something I have ever done (nor likely too), but use the code as is (changing names obviously), get that working then amend to suit.?
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 20:17
Joined
Jul 9, 2003
Messages
16,245
I tried the below codes with no success :(

You are more likely to get help if you communicate correctly! A blank statement that conveys no information is not very helpful to someone trying to diagnose your problem. I'm pretty sure you got an error message, and it would be good for you to report the error. The reason I think you got an error is message is because that looks like a label to me:- "LblOrderRef" and if you want to access what's in the label you would have to write it more like this:- "LblOrderRef.Caption"... Your code is assigning the object "LblOrderRef" and that would have caused an error message I'm sure...
 

Gasman

Enthusiastic Amateur
Local time
Today, 20:17
Joined
Sep 21, 2011
Messages
14,047
many thanks
I tried the below codes with no success :(, the webpage opens but does not populate the IDorder field on the webpage

Code:
Doc.getElementById("IDorderRef").Value = LblOrderRef
Well FWIW I just amended and used that code to put username and password into the Argos login page.?

Haven't worked out what the button name is to click, but your SendKeys may work just as well.?
 

stu_c

Registered User.
Local time
Today, 20:17
Joined
Sep 20, 2007
Messages
489
Hello Gasman
thanks again for your help, tried the below code and once again the webpage loads up but nothing else happens no errors or anything

Code:
    blnOpening = True
    Do While blnOpening = True
    blnOpening = False

Loop

DoEvents

   Doc.getElementById("IDorderRef").Value = LblOrderRef

   Exit Sub

    err_handler:
    If Err.Number = 5 Then 'Invalid procedure call or argument
    blnOpening = True

End If
Resume Next
End Sub
 

Gasman

Enthusiastic Amateur
Local time
Today, 20:17
Joined
Sep 21, 2011
Messages
14,047
Where is the rest of the code, particularly creating the Doc object.?
Walk through the code with F8 and inspect the variables.
 

stu_c

Registered User.
Local time
Today, 20:17
Joined
Sep 20, 2007
Messages
489
sorry, I missed this off by mistake, this is the module code

Code:
Option Compare Database
' Declare Type for API call:
      Private Type OSVERSIONINFO
        dwOSVersionInfoSize As Long
        dwMajorVersion As Long
        dwMinorVersion As Long
        dwBuildNumber As Long
        dwPlatformId As Long
        szCSDVersion As String * 128   '  Maintenance string for PSS usage
      End Type

      ' API declarations:
      Private Declare Function GetVersionEx Lib "kernel32" _
         Alias "GetVersionExA" _
         (lpVersionInformation As OSVERSIONINFO) As Long

      Private Declare Sub keybd_event Lib "user32" _
         (ByVal bVk As Byte, _
          ByVal bScan As Byte, _
          ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

      Private Declare Function GetKeyboardState Lib "user32" _
         (pbKeyState As Byte) As Long

      Private Declare Function SetKeyboardState Lib "user32" _
         (lppbKeyState As Byte) As Long

      ' Constant declarations:
      Const VK_NUMLOCK = &H90
      Const VK_SCROLL = &H91
      Const VK_CAPITAL = &H14
      Const KEYEVENTF_EXTENDEDKEY = &H1
      Const KEYEVENTF_KEYUP = &H2
      Const VER_PLATFORM_WIN32_NT = 2
      Const VER_PLATFORM_WIN32_WINDOWS = 1

Function IsCapsLockOn() As Boolean
        Dim o As OSVERSIONINFO

        o.dwOSVersionInfoSize = Len(o)
        GetVersionEx o
        Dim keys(0 To 255) As Byte
        GetKeyboardState keys(0)
        IsCapsLockOn = keys(VK_CAPITAL)
End Function

Sub ToggleCapsLock()
        Dim o As OSVERSIONINFO

        o.dwOSVersionInfoSize = Len(o)
        GetVersionEx o
        Dim keys(0 To 255) As Byte
        GetKeyboardState keys(0)

        If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then  '=====Win95
        'Toggle capslock
            keys(VK_CAPITAL) = Abs(Not keys(VK_CAPITAL))
            SetKeyboardState keys(0)
        ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then   '=====WinNT
          'Simulate Key Press>
            keybd_event VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
          'Simulate Key Release
            keybd_event VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY _
               Or KEYEVENTF_KEYUP, 0
        End If
End Sub

Function IsNumLockOn() As Boolean
        Dim o As OSVERSIONINFO
        
        o.dwOSVersionInfoSize = Len(o)
        GetVersionEx o
        Dim keys(0 To 255) As Byte
        GetKeyboardState keys(0)
        IsNumLockOn = keys(VK_NUMLOCK)
End Function

Sub ToggleNumLock()
        Dim o As OSVERSIONINFO
                
        o.dwOSVersionInfoSize = Len(o)
        GetVersionEx o
        Dim keys(0 To 255) As Byte
        GetKeyboardState keys(0)

          If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then  '=====Win95
                keys(VK_NUMLOCK) = Abs(Not keys(VK_NUMLOCK))
                SetKeyboardState keys(0)
          ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then   '=====WinNT
          'Simulate Key Press
            keybd_event VK_NUMLOCK, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
          'Simulate Key Release
            keybd_event VK_NUMLOCK, &H45, KEYEVENTF_EXTENDEDKEY _
               Or KEYEVENTF_KEYUP, 0
          End If
        
End Sub

Function IsScrollLockOn()
        Dim o As OSVERSIONINFO
        
        o.dwOSVersionInfoSize = Len(o)
        GetVersionEx o
        Dim keys(0 To 255) As Byte
        GetKeyboardState keys(0)
        IsScrollLockOn = keys(VK_SCROLL)
End Function

Sub ToggleScrollLock()
        Dim o As OSVERSIONINFO
        
        o.dwOSVersionInfoSize = Len(o)
        GetVersionEx o
        Dim keys(0 To 255) As Byte
        GetKeyboardState keys(0)
        If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then  '=====Win95
            keys(VK_SCROLL) = Abs(Not keys(VK_SCROLL))
            SetKeyboardState keys(0)
        ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then   '=====WinNT
            'Simulate Key Press
            keybd_event VK_SCROLL, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
            'Simulate Key Release
            keybd_event VK_SCROLL, &H45, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0
        End If
End Sub

Sub mySendKeys(sKeys As String, Optional bWait As Boolean = False)
Dim bNumLockState As Boolean
Dim bCapsLockState As Boolean
Dim bScrollLockState As Boolean
    bNumLockState = IsNumLockOn()
    bCapsLockState = IsCapsLockOn()
    bScrollLockState = IsScrollLockOn()
    SendKeys sKeys, bWait
    If IsNumLockOn() <> bNumLockState Then
        ToggleNumLock
    End If
    If IsCapsLockOn() <> bCapsLockState Then
        ToggleCapsLock
    End If
    If IsScrollLockOn() <> bScrollLockState Then
        ToggleScrollLock
    End If
End Sub

Function fSendKeys(sKeys As String, Optional bWait As Boolean = False)
' Function to make it callable from macros
    mySendKeys sKeys, bWait
End Function

Where is the rest of the code, particularly creating the Doc object.?
Walk through the code with F8 and inspect the variables.
 

stu_c

Registered User.
Local time
Today, 20:17
Joined
Sep 20, 2007
Messages
489
Still really struggling to get the IDs to work rather than tabbing,
 

Gasman

Enthusiastic Amateur
Local time
Today, 20:17
Joined
Sep 21, 2011
Messages
14,047
I honestly cannot make head or tail of that, and I do not think it is relevant yet. if at all?
As I mentioned I managed to complete both fields on a webpage. I just did not know how to emualte Enter or get the button click event to run.

This post was about completing the fields without tabbing through them in sequence?
 

stu_c

Registered User.
Local time
Today, 20:17
Joined
Sep 20, 2007
Messages
489
Afternoon Gasman
the reason I am using tabs is that when it loads up the webpage it starts at one point and does multiple tabs till it gets to the correct field, this is the only way I was able to get the information into the correct field, ideally what I want is Access to input the data into the correct IDfield on the webpage using the unique ID tag but I wasn't able to get this to work hence why I used tabs, I am not bothered about the enter to click a button its purely getting the information from the Access form onto the webpage form

I honestly cannot make head or tail of that, and I do not think it is relevant yet. if at all?
As I mentioned I managed to complete both fields on a webpage. I just did not know how to emualte Enter or get the button click event to run.

This post was about completing the fields without tabbing through them in sequence?
 

Gasman

Enthusiastic Amateur
Local time
Today, 20:17
Joined
Sep 21, 2011
Messages
14,047
Yes, I understand that, and I linked to code that would put the values in the page IF you knew the ID of each, which you said you did.?
I tested a small routine on an Argos page I use all the time and manages successfully for the two fields on that page.
I could not find however what the button was called to initiate it's click event.

However,I proved that the code would work.? Admittedly I used IE, but I dare say if you get it working, you could then try Chrome or whatever other browser you use.

FWIW I have never done anything like this before, so was just taking it step by step.?

The page might not even allow it.? I know some pages do not allow pasting into a field (generally when you have to confirm your email address). I do not know if setting the field is seen as pasting.?
 

stu_c

Registered User.
Local time
Today, 20:17
Joined
Sep 20, 2007
Messages
489
Hello Mate
I am thankful for your assistance but even when I use the code from the link you provided is still was not working unfortunately, I am also using IE so not sure that would be an issue, also I am sure it allows pasting as I am able to do it using the tab technique, would you be able to provide some light in your code so I can compare?

Yes, I understand that, and I linked to code that would put the values in the page IF you knew the ID of each, which you said you did.?
I tested a small routine on an Argos page I use all the time and manages successfully for the two fields on that page.
I could not find however what the button was called to initiate it's click event.

However,I proved that the code would work.? Admittedly I used IE, but I dare say if you get it working, you could then try Chrome or whatever other browser you use.

FWIW I have never done anything like this before, so was just taking it step by step.?

The page might not even allow it.? I know some pages do not allow pasting into a field (generally when you have to confirm your email address). I do not know if setting the field is seen as pasting.?
 

Gasman

Enthusiastic Amateur
Local time
Today, 20:17
Joined
Sep 21, 2011
Messages
14,047
I just used the code and modified it to fit a test workbook of mine.?
Admittedly I did the test in Excel as that was the program the code referred to, but I was only testing the logic.?
I only tested to the Sendkeys line, that appeared to be executed in the VBA window, not the browser.?
Code:
Sub DynamicWebPage()

' here I define elemnts for the loop
Dim sht As Worksheet
Set sht = ThisWorkbook.Sheets("data")
Dim LastRow As Long
Dim i As Long
LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row

' here I define Internet Explorer
Dim IE As Object, objShell As Object
Dim Doc As HTMLDocument
Set IE = CreateObject("InternetExplorer.Application")

' here I define Object to sendkeys
Dim SHELL_OBJECT
SHELL_OBJECT = "WScript.Shell"
Set objShell = CreateObject(SHELL_OBJECT)

' here I define range for the loop
For i = 2 To LastRow

' here I ask the Internet explorer to be visable & Navigate to value in cell "H"i
IE.Visible = True
IE.navigate sht.Range("H" & i).Value

' here I ask the Internet explorer to wait few seconds
Do While IE.Busy
    Application.Wait DateAdd("s", 5, Now)
Loop
IE.navigate "https://www.argos.co.uk/account/login?clickOrigin=header:account"

Set Doc = IE.document

'******* From here the macro fill the data from excel table in range into WEB elements******
Doc.getElementById("emailAddress").Value = sht.Range("A" & i).Value
Doc.getElementById("currentPassword").Value = sht.Range("B" & i).Value
Application.Wait DateAdd("s", 1, Now)
'Doc.getElementById("customer|UserDefinedIdTA").Focus
objShell.SendKeys "{Enter}"


Application.Wait DateAdd("s", 5, Now)
'Doc.getElementById("selEngagement").Value = sht.Range("C" & i).Value
'date format should be m/d/yyyy
'Doc.getElementById("txtPlannedStart").Value = sht.Range("D" & i).Value
'Doc.getElementById("Project|ProjTimeAppTA").Value = sht.Range("E" & i).Value
'Doc.getElementById("Project|SecondProjTimeAppTA").Value = sht.Range("F" & i).Value
Application.Wait DateAdd("s", 5, Now)
Doc.getElementById("Button").Click

' here I ask to populate column "G"i with "created" string to know that this raw was successfully done
sht.Range("G" & i).Value = "Created"

Next i
MsgBox "Process 100% completed"

End Sub

1589560933889.png
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 04:17
Joined
May 7, 2009
Messages
19,169
on your reply (post #):

lblOrderRef, is this a Label control?
if it is you should use it's Caption property:

Doc.getElementById("IDorderRef").Value = [LblOrderRef].Caption
 

stu_c

Registered User.
Local time
Today, 20:17
Joined
Sep 20, 2007
Messages
489
Hello sorry for the late reply I haven't been able to get near a computer lately,
where would I put the code you have provided in my code above? reason I ask when I try this ir comes back with an error saying
"Complie Error
Method or Data member not found"

this is my code I tried

Code:
Private Sub Command1764_Click()
Dim strLink As String
    Dim blnOpening As Boolean
    Dim strWebTitle As String
    On Error GoTo err_handler
    
    strLink = "http://EXAMPLE-WEBSITE.co.uk"
    strLink = "C:\Program Files\Internet Explorer\IEXPLORE.EXE " & strLink
 
    Call Shell(strLink, vbMaximizedFocus)
    
    blnOpening = True
    Do While blnOpening = True
    blnOpening = False
    
Loop
    Dim started As Single: started = Timer
    Do: DoEvents: Loop Until Timer - started >= 1
 
DoEvents
 Doc.getElementById("IDorderRef").Value = [LblOrderRef].Caption
Exit Sub
    
err_handler:
    If Err.Number = 5 Then 'Invalid procedure call or argument
        blnOpening = True

End If
Resume Next

End Sub

on your reply (post #):

lblOrderRef, is this a Label control?
if it is you should use it's Caption property:

Doc.getElementById("IDorderRef").Value = [LblOrderRef].Caption
 

Users who are viewing this thread

Top Bottom