複雑な手順を伴う単純なタスクがあります。アプリケーションの使用状況やその他の関連情報を追跡する追跡プログラムを作成しています。最初に、データが取得されると削除される一時テキスト ファイルに、アプリケーションからのデータを記録しています。CSV ファイルに保存できるように、データはカンマで区切られます。この CSV ファイルは、確認のために特定の情報をすばやく取得するために使用されます。データは、2010 Access データベースにも永続的に保存されます。テキストファイルにデータを保存できました。テキスト ファイルからデータを読み取り、それを CSV ファイルにコピーする VBScript を作成できました。私がしなければならないことは、データベースにデータを挿入するスクリプトの上にメッセージ ボックスを配置すると、メッセージ ボックスに情報が表示されるのに、それが表示されない理由を理解することです。
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 で SQL を書いたことはなく、インターネットで見つけたものはすべて機能しません。