2017-08-07 26 views
-1

我無法將讀入strTextLine的內容轉換爲SQL在CurrentDb.Execute strSql,dbFailOnError中查找的單個「VALUES」。通過CurrentDb.Execute將文本字符串寫入表strSql

Public Function ReadTextFile() 

Dim objFSO As Object 
Dim objTextStream As Object 
Dim strTextLine As String 
Dim strInputFileName As String, strSql As String, strLine As String, x As Integer 

strInputFileName = "F:\Test\Input.txt" 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objTextStream = objFSO.OpenTextFile(strInputFileName) 

Do While Not (objTextStream.AtEndOfStream) 

strTextLine = objTextStream.ReadLine 

    If Left(strTextLine, 4) = "1RUN" Then 
     x = 0 
    Else 
     If x < 9 Then 
     Else 
      Debug.Print strTextLine, Values 
      'strSql = "INSERT INTO tmp_tbl_load_loc_match (field1, field 2, etc ..)Values (???" 
      'CurrentDb.Execute strSql, dbFailOnError 
     End If 
    End If 

x = x + 1 

Loop 

objTextStream.Close 
Set objFSO = Nothing 
Set objTextStream = Nothing 

End Function 
+0

呃......我不知道你的意思是「不能把什麼讀到字符串中」。請澄清。你想插入什麼,出了什麼問題? –

+0

您的strSql只是完整的SQL命令的第一部分。所有的值都丟失了。因此請閱讀SQL語法。 – Gustav

+0

我知道這些值是空白的,就是我難住的地方。如何將strTextLine分段(轉換)爲值? – Kaw4Life

回答

0

寫了一個函數將字符串解析爲單個字段,然後附加到SQL的VALUES部分。

STRSQL = funInsertSql(strTextLine) CurrentDb.Execute STRSQL,dbFailOnError

我的一部分時間程序員。雖然有人會一直這樣做可以幫助或至少參與一些對話來幫助解決問題。