0

首先,我是 iMacros 的新手,并且对 VBA 不是很好(我知道这不是一个好的开始)所以我的最终游戏是使用 iMacros 去一个站点在站点上填写一个表格,其中包含访问表中的名称输入名称并从该站点抓取一些结果文本 抓取文本并将其放在表格中。我将不得不为表中的每条记录执行此操作。到目前为止,这就是我所拥有的:

Dim Rs As DAO.Recordset         'recordset for list of names from VcWAuditUsers
Dim db As DAO.Database
Dim SQL As String
Dim Sql2 As String
Dim STRErr As String
Dim sTableName As String
Dim serverName As String
Dim dbName As String
Dim strUserCnt As Integer
Dim UserName As Variant
Dim StrSql As String


    Dim iim1, iret

    Set iim1 = CreateObject("imacros")
    iret = iim1.iimInit

    iret = iim1.iimPlayCode("URL GOTO=https://www.sam.gov/portal/public/SAM/)

sTableName = "vCPpAuditUsers"
serverName = GetLinkedServer(sTableName)
dbName = GetLinkedDatabase(sTableName)
SQL = "Select Distinct FName, LName from " & sTableName


Set db = CurrentDb
Set Rs = db.OpenRecordset(SQL)

If (Not Rs.EOF And Not Rs.BOF) Then
Rs.MoveLast
Rs.MoveFirst

With Rs
   Do While (Rs.EOF = False)



                UserName = Trim(![FName]) & " " & Trim(![LName])
                    MsgBox ("New Name: " & UserName)


                strUserCnt = Rs.recordCount
                    MsgBox ("Number of rows: " & strUserCnt)


                    'set iMacros variables
                iret = iim1.iimSet("CONTENT", UserName)
                iret = iim1.iimPlay("Y:\Data\FS01-M\Healthcare\SAM_iMacro\SAMiMacro.iim")

                    If iret < 0 Then
                        MsgBox iim1.iimGetLastError()
                    End If

              StrSql = "Insert Into ExceptionResults Values('" & UserName & "','" & iim1.iimGetExtract(1) & Now & "')"
                MsgBox ("Test SqlInsert: " & StrSql)

        .MoveNext
        Loop
    End With


    Rs.Close
    db.Close

End If

我知道我错过了一些关键的东西,但我一直无法找到一个很好的例子来作为我正在做的事情的基础。

任何帮助是极大的赞赏!

谢谢。

4

1 回答 1

0

我想出了:选项比较数据库选项显式

    Private Sub cmdGetExceptions_Click()


    Dim YNMess As String




        YNMess = MsgBox("Do you wish to truncate results table ExceptionResults?", vbYesNo, "TRUNCATE?")

            If YNMess = vbYes Then
                Call ClearExceptionTable
                Call RunExceptionTable
            End If
            If YNMess = vbNo Then
                Call RunExceptionTable
            End If

   End Sub


  Private Sub RunExceptionTable()

Dim Rs As DAO.Recordset         'recordset for list of names from VcWAuditUsers
Dim db As DAO.Database
Dim SQL As String
Dim sTableName As String
Dim serverName As String
Dim dbName As String
Dim strUserCnt As Integer
Dim UserName As Variant
Dim StrSql As String
Dim ExceptStat As String

  On Error GoTo ErrHandler

Dim iim1, iret

' Creates iMacros object and gives the starting webpage
Set iim1 = CreateObject("imacros")
iret = iim1.iimInit
iret = iim1.iimPlayCode("URL GOTO=https://www.sam.gov/)


'Sets the source table name
sTableName = "[SourceTable]"

'Sets the SQL string to grab the names of people to be inserted into website input section
SQL = "Select Distinct FName, LName  from " & sTableName

'Starts the recordset for the source table and recordset
Set db = CurrentDb
Set Rs = db.OpenRecordset(SQL)

'resets the RS to start at the begining
If (Not Rs.EOF And Not Rs.BOF) Then
    Rs.MoveLast
    Rs.MoveFirst

    'Grabs the total record count to use for end user messaging.
strUserCnt = Rs.recordCount
    'MsgBox ("Number of rows: " & strUserCnt)

    'Opens RS and starts while loop to open first record of the source table
    With Rs
       Do While (Rs.EOF = False)

                    'Creates new UserName by combining first and last name
                    UserName = Trim(![FName]) & " " & Trim(![LName])
                    'MsgBox ("New Name: " & UserName)


                    'set iMacros variables This subs the spot in the iMacros code where you manually entered information (there should be {{USERNAME}} in the iMacros where you want to enter data.
                iret = iim1.iimSet("USERNAME", UserName)
                    'Plays the iMacro you recorded and altered earlier.
                iret = iim1.iimPlay("Location of your iMacros goes here.iim")

                        'Checks for errors in the iMacros(anything in the negative is considered an error)
                    If iret < 0 Then
                        MsgBox iim1.iimGetLastError()
                    End If

                        'grabs the extracted data from recorded iMacro. the extracted data is stored in an (1) based array. Makes substitutions for the text that it extracts to convert to 1 or 0
                    If Left(iim1.iimGetExtract(1), 2) = "No" Then
                        ExceptStat = 0
                    Else
                        ExceptStat = 1
                    End If


                    'For each record in the source the extracted data is entered into the insert statement below along with the employee name and date.  then warnings are suppressed and each is inserted into a local access table, Loop and move to the next.
              StrSql = "Insert Into ExceptionResults Values('" & UserName & "'," & ExceptStat & ",'" & Now & "')"
                DoCmd.SetWarnings False
                DoCmd.RunSQL (StrSql)
                DoCmd.SetWarnings True
              .MoveNext
        Loop
    End With

MsgBox ("ExceptionResults table is complete and has " & strUserCnt & " Records")

        'Clean up
    Rs.Close
    db.Close

End If

'Clean up
Set db = CurrentDb
Set Rs = db.OpenRecordset(SQL)
Set iim1 = Nothing
strUserCnt = 0

ErrHandler:
    MsgBox "ERROR" & vbCrLf & Err.Description, vbCritical, "CmdGetExceptions"
End Sub

Private Sub ClearExceptionTable()

Dim StrSql2 As String

StrSql2 = "Delete from ExceptionResults"


    DoCmd.SetWarnings False
    DoCmd.RunSQL (StrSql2)
    DoCmd.SetWarnings True

    MsgBox ("All records from ExceptionResults have been truncated")
End Sub
于 2013-11-14T21:01:25.467 回答