我有一个复杂程序的简单任务。我正在创建跟踪程序,跟踪应用程序的使用情况以及其他相关信息。我首先将应用程序中的数据记录在临时文本文件中,一旦检索到数据,该文件将被删除。数据以逗号分隔,以便存储在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中写过,我在互联网上找到的所有内容都不起作用。
答案 0 :(得分:2)
您定义了strSQL,但不执行SQL语句,添加
conn.Execute strSQL
在strSQL = ...
行之后
答案 1 :(得分:1)
除了roland已经指出的内容之外,VBScript不会在字符串中扩展变量。 strSplitData(0)
中的"... VALUES (strSplitData(0), ...)"
只是文字字符串"strSplitData(0)"
,而不是数组strSplitData
的第一个字段的值。您可以通过串联构建查询字符串,如下所示:
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) & ")"
然而,这样做not a good idea,所以忘记我提到它。更好地使用parameterized query(AKA准备好的声明):
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
根据需要调整CreateParameter()
来电的参数。