2017-05-19 98 views
0

我試圖從Access DB發送數據到網站http://www.lee.vote/voters/check-your-registration-status/。我可以使用類似的代碼(如下)將數據發送到不同的網站,但我無法弄清楚爲什麼它不適用於這個網站。使用MS Access VBA發送數據到網絡表格

從我嘗試填寫HTML:

<div id="FindVoterForm"> 
    <div id="IntroText"> 
    <h1 style="text-align: center; margin-bottom: 3px;">Voter Information in&nbsp;<span id="MainCounty">Lee</span>&nbsp;County</h1> 
     <h2 style="text-align: center; margin-top: 3px; margin-bottom: 3px;">Sample Ballots and Voting Locations</h2> 
    <span class="style1" style="margin-bottom: 0px;">Complete the form to see:</span><ul style="margin-top: 0px;"> 
     <li class="style1"><b>Where to vote on election day</b></li> 
     <li class="style1"><b>Sample ballots</b></li> 
     <li class="style1"><b>Upcoming elections</b></li> 
    </ul> 
    <p class="style2" style="margin-bottom: 0px;"> 
     You'll also be able to:</p> 
    <ul style="margin-top: 0px;"> 
     <li class="style2">Request a mail ballot</li> 
      <li class="style2">Review/update your voter registration information</li> 
      <li class="style2">Check the status of your mail ballot</li> 
      <li class="style2">Review your voting activity for the past 12 months</li> 
    </ul> 
    <div id="NotRegistered" style="font-size: small;"><a href="https://www.voterfocus.com/ws/pfinder/printvapp4.php?county=lee" target="_blank">If you are not registered to vote please fill out our voter registration form</a></div><br> 
    <i><b style="text-decoration: underline;">All items are required</b></i>. 
    </div> 
    <div class="voterForm"> 
    <div class="voterFormLine"><div>1.</div><div>Voter's Last Name</div><div><input title="Please enter your last name." id="NameID" type="text" size="10" maxlength="35" value=""></div> 
    </div><div class="voterFormLine"><div>2.</div><div>Voter's Birth Date</div><div><input title="Please enter your birth date (MM/DD/YYYY)." id="BirthDate" type="text" size="10" maxlength="10" value=""> 
    <br>MM/DD/YYYY</div></div><div class="voterFormLine"><div>3.</div> 
    <div><a title="House Number" href="https://www.voterfocus.com/VFVoterGlossery.php?term=House Number" target="_blank">House Number</a> of Voter's Residence Address</div> 
    <div><input title="Please enter your house street number." id="StNumber" type="text" size="10" maxlength="10" value=""></div> 
    </div> 
    <div>&nbsp;</div> 
    </div> 
    <div><div style="text-align: center;"><h2 id="MoreVoter" style="display: none;"><b></b></h2> 
    <button id="ButtonForm" onclick="ButtonForm_onclick()" type="button" value="Submit">Submit</button></div> 
    </div> 
    </div> 


VBA代碼:

'creates a new internet explorer window 
Dim IE As Object 
Set IE = CreateObject("InternetExplorer.Application") 

'opens Lee County registration check 
With IE 
    .Visible = True 
    .navigate "http://www.lee.vote/voters/check-your-registration-status/" 
End With 

'waits until IE is loaded 
Do Until IE.ReadyState = 4 And Not IE.busy 
    DoEvents 
Loop 

'sends data to the webpage 
Call IE.Document.getelementbyid("NameID").setattribute("value", Last_Name) 
Call IE.Document.getelementbyid("BirthDate").setattribute("value", Date_of_Birth.Value) 
Call IE.Document.getelementbyid("StNumber").setattribute("value", Street_Number.Value) 

'"clicks" the button to display the results 
IE.Document.getelementbyid("ButtonForm").Click 

任何幫助嗎?

+1

究竟不起作用?你可以做任何與網頁的互動或根本沒有? –

+0

不是這個網頁。我無法將Access中的數據(字符串變量)傳遞到網頁上的表單字段。 – Dave

+0

你可以與網頁互動嗎?例如從它讀取數據? –

回答

0

這是我想出了(急需)幫助確定我是不是真的導航到合適的網頁的形式後回答:

'creates a new internet explorer window 
Dim IE As Object 
Set IE = CreateObject("InternetExplorer.Application") 

'opens Lee County registration check 
With IE 
    .Visible = True 
    .navigate "https://www.electionsfl.org/VoterInfo/vflookup.html?county=lee" 
End With 

'waits until IE is loaded 
Do Until IE.ReadyState = 4 And Not IE.busy 
    DoEvents 
Loop 
x = Timer + 2 
Do While Timer < x 
    DoEvents 
Loop 

'sends data to the webpage 
Call IE.Document.getelementbyid("NameID").setattribute("value", Last_Name.Value) 
    'formats DOB to correct output 
    Dim DOBMonth As Integer 
    Dim DOBDay As Integer 
    Dim DOBYear As Integer 
    DOBMonth = Month(Date_of_Birth.Value) 
    DOBDay = Day(Date_of_Birth.Value) 
    DOBYear = Year(Date_of_Birth.Value) 

    If DOBMonth < 10 Then 
     Call IE.Document.getelementbyid("BirthDate").setattribute("value", "0" & DOBMonth & "/" & DOBDay & "/" & DOBYear) 
    Else 
     Call IE.Document.getelementbyid("BirthDate").setattribute("value", DOBMonth & "/" & DOBDay & "/" & DOBYear) 
    End If 
Call IE.Document.getelementbyid("StNumber").setattribute("value", Street_Number.Value) 

'"clicks" the button to display the results 
IE.Document.getelementbyid("ButtonForm").Click 
0

您提供的HTML代碼段屬於iframe <iframe id="dnn_ctr1579_View_VoterLookupFrame" src="https://www.electionsfl.org/VoterInfo/vflookup.html?county=lee" width="100%" height="2000" frameborder="0"></iframe>,因此您應該導航到URL https://www.electionsfl.org/VoterInfo/vflookup.html?county=lee而不是http://www.lee.vote/voters/check-your-registration-status/

我在Chrome導航https://www.electionsfl.org/VoterInfo/vflookup.html?county=lee並檢查XHR記錄我經由開發者工具(F12),網絡標籤提交數據後:

xhr

似乎是與有效載荷在簡單的POST XML HTTP請求JSON格式,如:

{'LastName':'Doe', 'BirthDate':'01/01/1980', 'StNumber':'10025', 'County':'lee', 'FirstName':'', 'challengeValue':'', 'responseValue':''} 

這XHR不使用cookie或既不是頭也不載荷任何其他授權數據,所以我試圖重現使用下面的代碼相同的請求:

Option Explicit 

Sub Test_Submit_VoterInfo() 

    Dim sLastName As String 
    Dim sBirthDate As String 
    Dim sStNumber As String 
    Dim sFormData As String 
    Dim bytFormData 
    Dim sContent As String 

    ' Put the necessary data here 
    sLastName = "Doe" 
    sBirthDate = "01/01/1980" 
    sStNumber = "10025" 
    ' Combine form payload 
    sFormData = "{" & _ 
    "'LastName':'" & sLastName & "', " & _ 
    "'BirthDate':'" & sBirthDate & "', " & _ 
    "'StNumber':'" & sStNumber & "', " & _ 
    "'County':'lee', " & _ 
    "'FirstName':'', " & _ 
    "'challengeValue':'', " & _ 
    "'responseValue':''" & _ 
    "}" 
    ' Convert string to UTF-8 binary 
    With CreateObject("ADODB.Stream") 
     .Open 
     .Type = 2 ' adTypeText 
     .Charset = "UTF-8" 
     .WriteText sFormData 
     .Position = 0 
     .Type = 1 ' adTypeBinary 
     .Position = 3 ' skip BOM 
     bytFormData = .Read 
     .Close 
    End With 
    ' Make POST XHR 
    With CreateObject("MSXML2.XMLHTTP") 
     .Open "POST", "https://www.electionsfl.org/VoterInfo/asmx/service1.asmx/FindVoter", False, "u051772", "mar4fy16" 
     .SetRequestHeader "Content-Length", LenB(bytFormData) 
     .SetRequestHeader "Content-Type", "application/json; charset=UTF-8" 
     .Send bytFormData 
     sContent = .ResponseText 
    End With 
    ' Show response 
    Debug.Print sContent 

End Sub 

對我的反應是{"d":"[]"},一樣的瀏覽器,但不幸的是,我不能,檢查它是否在服務器上處理正確,因爲我沒有有效投票記錄數據。

+0

這沒有奏效。它沒有返回任何錯誤信息,但它似乎也沒有與網頁互動。 – Dave