0

我有一个简单的任务和一个复杂的过程。我正在创建一个跟踪程序来跟踪应用程序的使用情况以及其他相关信息。我首先将应用程序中的数据记录在一个临时文本文件中,一旦检索到数据,该文件将被删除。数据以逗号分隔,以便可以存储在 CSV 文件中。此 CSV 文件将用于快速提取特定信息以供审核。数据还将永久存储在 2010 Access 数据库中。我已经能够将数据存储在文本文件中。我已经能够创建一个 VBScript 来从文本文件中读取数据并将其复制到 CSV 文件中。我需要做的是弄清楚为什么,当我在将数据插入数据库的脚本上方放置一个消息框时,我可以在消息框中看到信息,但它不会

这是 VBScript 代码:

' Set constants for reading, writing, and appending files
Const ForReading = 1, ForWriting = 2, ForAppending = 8

' Sets up the object variables.
Dim objExcel, objFSO, objTextFile, objCSVFile, objTrackingFolder

' Sets up the integer variables.
Dim intPathYPos

' Sets up the all the string variables for the program.
Dim Desktop, todaysDate, usageDate, myDay, myMonth, myYear, UserIDPath, myMessage
Dim strTextFile, strHeadLine, strTextLine, strCSVFile, UserID, strTrackingFolder, strConnect, strSQL, strSplitData, testDisplay
Dim message

'This creates the required Objects
Set objExcel = CreateObject("Excel.application")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set conn = CreateObject("ADODB.Connection")
Set WshShell = WScript.CreateObject("WScript.Shell")
Desktop = WshShell.ExpandEnvironmentStrings("%USERPROFILE%") & "\" & "Desktop"
UserIDPath = WshShell.ExpandEnvironmentStrings("%USERPROFILE%")

'------------------Calls up the Process Tracking Submission sub-------------------

call ProcessTrackingSubmission

sub ProcessTrackingSubmission()

intPathYPos = InStr(4,UserIDPath,"\")
intPathYPos = intPathYPos + 1
UserID = Mid(UserIDPath, intPathYPos, 10)

'msgbox(RTrim(UserID))
'exit sub

' Set date for date stamp in file name and sheet name
todaysDate = Date()

myMonth = Month(todaysDate)
If Len(myMonth)=1 Then myMonth="0" & myMonth

myDay = Day(todaysDate)
If Len(myDay)=1 Then myDay="0" & myDay

myYear = Right(Year(todaysDate), 2)

usageDate = myMonth & myDay & myYear

' Set up the origin and destination files
strTextFile = Desktop & "\MacroTracker.txt"
strTrackingFolder = "E:\My Storage Files\" & UserID
strCSVFile = strTrackingFolder & "\TrackingTesting" & usageDate & ".csv"
strHeadLine = "App Name,User ID,Ran At,Data 1,Data 2,Data 3,Data 4,Data 5,Data 6,Data 7,Data 8"

Set objTextFile = objFSO.OpenTextFile(strTextFile)

Wscript.Sleep 600

' Read the entire origin file
Do Until objTextFile.AtEndOfStream
    strTextLine = objTextFile.ReadLine
Loop

Wscript.Sleep 600

objTextFile.Close

If (objFSO.FolderExists(strTrackingFolder)) Then
    If (objFSO.FileExists(strCSVFile)) Then
        ' Create object for appending current TXT file to CSV file
        Set objCSVFile = objFSO.OpenTextFile(strCSVFile, ForAppending, True)
    Else
        ' Create CSV file to write to with today's date
        Set objCSVFile = objFSO.CreateTextFile(strCSVFile, True)
        Wscript.Sleep 1000
        ' Write initial header for the CSV file
        objCSVFile.WriteLine strHeadLine
    End If
Else
    Set objTrackingFolder = objFSO.CreateFolder(strTrackingFolder)
    If (objFSO.FileExists(strCSVFile)) Then
        ' Create object for appending current TXT file to CSV file
        Set objCSVFile = objFSO.OpenTextFile(strCSVFile, ForAppending, True)
    Else
        ' Create CSV file to write to with today's date
        Set objCSVFile = objFSO.CreateTextFile(strCSVFile, True)
        Wscript.Sleep 1000
        ' Write initial header for the CSV file
        objCSVFile.WriteLine strHeadLine
    End If
End If

' Write an append line of data to the CSV file
objCSVFile.WriteLine strTextLine

 Wscript.Sleep 600

strDataLine = Split(strTextLine, ",")
strAppName = strDataLine(0)
strUserID = strDataLine(1)
strRanAt = strDataLine(2)
strData1 = strDataLine(3)
strData2 = strDataLine(4)
strData3 = strDataLine(5)
strData4 = strDataLine(6)
strData5 = strDataLine(7)
strData6 = strDataLine(8)
strData7 = strDataLine(9)
strData8 = strDataLine(10)

' Connect to the database
strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\My Storage Files\Tracking Apps.mdb"
conn.Open strConnect

Wscript.Sleep 600

' Write data to table
    if strAppName = "Hello Application - version 1" Then
strSQL = "INSERT INTO [Macro Tracking] ([App Name], [User ID], [Ran At], [Data 2], [Data 6]) VALUES ([" & strAppName & "], [" & strUserID & "], [" & strRanAt & "], [" & strData2 & "], [" & strData6 & "])"
    end if

Wscript.Sleep 600

objCSVFile.Close
conn.Close

' Wait for file to be written to
Wscript.Sleep 600

' Delete origin file to prevent user tampering
objFSO.DeleteFile(strTextFile)

end sub

任何帮助将不胜感激。我使用过 HTML 数据库,所以我知道 SQL 应该是什么样子,但我从来没有用 VBScript 编写过,而且我在 Internet 上找到的所有内容都不起作用。

4

2 回答 2

2

你定义了strSQL,但不执行SQL语句,添加

conn.Execute strSQL

strSQL = ...行后

于 2013-07-31T13:02:14.967 回答
1

In addition to what roland already pointed out, VBScript does not expand variables inside strings. strSplitData(0) in "... VALUES (strSplitData(0), ...)" is just the literal string "strSplitData(0)", not the value of the first field of the array strSplitData. You could build the query string by concatenation like this:

strSQL = "INSERT INTO [Macro Tracking] " & _
         "([App Name], [User ID], [Ran At], [Data 1], [Data 2], " & _
         "[Data 3], [Data 4], [Data 5], [Data 6], [Data 7], [Data 8]) " & _
         "VALUES (" & _
         strSplitData(0) & ", " & _
         strSplitData(1) & ", " & _
         strSplitData(2) & ", " & _
         strSplitData(3) & ", " & _
         strSplitData(4) & ", " & _
         strSplitData(5) & ", " & _
         strSplitData(6) & ", " & _
         strSplitData(7) & ", " & _
         strSplitData(8) & ", " & _
         strSplitData(9) & ", " & _
         strSplitData(10) & ")"

However, doing this is not a good idea, so just forget I mentioned it. Better use a parameterized query (AKA prepared statement) instead:

db = "E:\My Storage Files\TrackingApps.mdb"
strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & db & _
             ";User Id=admin;Password=;"

Set conn = CreateObject("ADODB.Connection")
conn.Open strConnect

Set cmd = CreateObject("ADODB.Command")
Set cmd.ActiveConnection = conn
cmd.CommandText = "INSERT INTO [Macro Tracking] ([App Name], [User ID], " & _
  "[Ran At], [Data 1], [Data 2], [Data 3], [Data 4], [Data 5], [Data 6], " & _
  "[Data 7], [Data 8]) VALUES (?,?,?,?,?,?,?,?,?,?,?)"

Set p1 = cmd.CreateParameter("@p1", 3, 1, 0, 0)
cmd.Parameters("@p1") = strSplitData(0)
cmd.Parameters.Append p1

Set p2 = cmd.CreateParameter("@p2", 3, 1, 0, 0)
cmd.Parameters("@p2") = strSplitData(1)
cmd.Parameters.Append p2

...

Set p11 = cmd.CreateParameter("@p11", 3, 1, 0, 0)
cmd.Parameters("@p11") = strSplitData(10)
cmd.Parameters.Append p11

cmd.Execute

Adjust the arguments of the CreateParameter() calls as needed.

于 2013-07-31T16:23:27.527 回答