“预期:=”将记录集添加到 SQL Server 2005 数据库时 vb6 中出现编译错误
这里我在 vb6 中创建了记录集并在该 vb6 中存储值。我想将该记录集值写入数据库表。在执行该代码时,我收到类似“Expected:=”的编译错误。请参阅下面的代码。请让我知道我哪里做错了。我在 cmdCommand.Execute() 中遇到错误
With rcdDNE.Fields
.Append "RTN", adVarChar, 9
.Append "AccountNbr", adVarChar, 17
.Append "IndividualName", adVarChar, 22
.Append "FirstName", adVarChar, 50
.Append "MiddleName", adVarChar, 1
.Append "LastName", adVarChar, 50
.Append "Amount", adCurrency
End With
rcdDNE.Open
intFileNbr = FreeFile(1)
Open strFileName For Input As #intFileNbr Len = 95 ' Open file for input.
Do While Not EOF(intFileNbr)
Line Input #intFileNbr, strCurrentLine
If Mid(strCurrentLine, 1, 1) = 6 Then
strRoutingNbr = Mid(strCurrentLine, 4, 8)
strAcct = Mid(strCurrentLine, 13, 17)
strIndividualName = Trim(Mid(strCurrentLine, 55, 22))
strAmount = Trim(Mid(strCurrentLine, 30, 10))
strAmount = Left(strAmount, Len(strAmount) - 1)
curAmount = CCur(strAmount)
End If
' Add new record to temporary recordset
With rcdDNE
.AddNew
.Fields![RTN] = strRoutingNbr
.Fields![AccountNbr] = strAcct
.Fields![IndividualName] = strIndividualName
.Fields![Amount] = curAmount
.Update
End With
Loop
Close #intFileNbr
frmDNELoad.lblStatus.Caption = "Formatting Names..."
frmDNELoad.Refresh
DoEvents
' Parse the IndividualName field
rcdDNE.MoveFirst
Do Until rcdDNE.EOF
lngMidInitPos = 0
lngParsePos1 = 0
lngParsePos2 = 0
lngParsePos3 = 0
lngParsePos4 = 0
lngParsePos5 = 0
lngParsePos6 = 0
strParseString = ""
strParseFirstNm = ""
strParseMidInit = ""
strParseLastNam = ""
strParseString = Trim(rcdDNE.Fields![IndividualName])
' Replace double spaces (" ") with a single space (" ")
lngPos = InStr(1, strParseString, " ")
Do While lngPos
strParseString = Mid(strParseString, 1, lngPos - 1) & Mid(strParseString, lngPos + 1, Len(strParseString))
lngPos = InStr(1, strParseString, " ")
Loop
' Locate positions of remaining spaces
lngParsePos1 = InStr(1, strParseString, " ")
If lngParsePos1 = 0 Then
lngParsePos2 = 0
Else
lngParsePos2 = InStr(lngParsePos1 + 1, strParseString, " ")
End If
If lngParsePos2 = 0 Then
lngParsePos3 = 0
Else
lngParsePos3 = InStr(lngParsePos2 + 1, strParseString, " ")
End If
If lngParsePos3 = 0 Then
lngParsePos4 = 0
Else
lngParsePos4 = InStr(lngParsePos3 + 1, strParseString, " ")
End If
If lngParsePos4 = 0 Then
lngParsePos5 = 0
Else
lngParsePos5 = InStr(lngParsePos4 + 1, strParseString, " ")
End If
If lngParsePos5 = 0 Then
lngParsePos6 = 0
Else
lngParsePos6 = InStr(lngParsePos5 + 1, strParseString, " ")
End If
' Determine if Middle initial is present
If (lngParsePos3 - lngParsePos2) = 2 Then
lngMidInitPos = lngParsePos2 + 1
rcdDNE.Fields![MiddleName] = Mid(strParseString, lngMidInitPos, 1)
ElseIf (lngParsePos4 - lngParsePos3) = 2 Then
lngMidInitPos = lngParsePos3 + 1
rcdDNE.Fields![MiddleName] = Mid(strParseString, lngMidInitPos, 1)
ElseIf (lngParsePos5 - lngParsePos4) = 2 Then
lngMidInitPos = lngParsePos4 + 1
rcdDNE.Fields![MiddleName] = Mid(strParseString, lngMidInitPos, 1)
ElseIf (lngParsePos6 - lngParsePos5) = 2 Then
lngMidInitPos = lngParsePos5 + 1
rcdDNE.Fields![MiddleName] = Mid(strParseString, lngMidInitPos, 1)
ElseIf (lngParsePos2 - lngParsePos1) = 2 Then
lngMidInitPos = lngParsePos1 + 1
rcdDNE.Fields![MiddleName] = Mid(strParseString, lngMidInitPos, 1)
End If
' If there is a middle initial, everything to the left of it goes into the
' first name field, and everything to the right of it goes into the last
' name field. If there is no middle initial, everything after the first space
' goes into the last name field.
If lngMidInitPos <> 0 Then
rcdDNE.Fields![FirstName] = Trim(Left(strParseString, lngMidInitPos - 1))
rcdDNE.Fields![LastName] = Trim(Mid(strParseString, lngMidInitPos + 1, Len(strParseString)))
Else
rcdDNE.Fields![FirstName] = Trim(Left(strParseString, lngParsePos1))
rcdDNE.Fields![LastName] = Trim(Mid(strParseString, lngParsePos1 + 1, Len(strParseString)))
End If
rcdDNE.Update
rcdDNE.MoveNext
Loop
' Write records to Database
Call FindServerConnection_NoMsg
Dim cmdCommand As New ADODB.Command
If rcdDNE.EOF = False Then
rcdDNE.MoveFirst
cmdCommand.CommandText = "insert into DneFrc (RTN, AccountNbr, FirstName, MiddleName, LastName, Amount) values (RTN, AccountNbr, FirstName, MiddleName, LastName, Amount)"
cmdCommand.Execute()
rcdDNE.MoveNext
Loop Until rcdDNE.EOF = True
Here I created recordset in vb6 and store values in that vb6. i want to write that recordset values to database table. while executing that code i am getting compile error like "Expected:=". please see the code below. Please let me know where I am doing wrong. I am getting error in cmdCommand.Execute()
With rcdDNE.Fields
.Append "RTN", adVarChar, 9
.Append "AccountNbr", adVarChar, 17
.Append "IndividualName", adVarChar, 22
.Append "FirstName", adVarChar, 50
.Append "MiddleName", adVarChar, 1
.Append "LastName", adVarChar, 50
.Append "Amount", adCurrency
End With
rcdDNE.Open
intFileNbr = FreeFile(1)
Open strFileName For Input As #intFileNbr Len = 95 ' Open file for input.
Do While Not EOF(intFileNbr)
Line Input #intFileNbr, strCurrentLine
If Mid(strCurrentLine, 1, 1) = 6 Then
strRoutingNbr = Mid(strCurrentLine, 4, 8)
strAcct = Mid(strCurrentLine, 13, 17)
strIndividualName = Trim(Mid(strCurrentLine, 55, 22))
strAmount = Trim(Mid(strCurrentLine, 30, 10))
strAmount = Left(strAmount, Len(strAmount) - 1)
curAmount = CCur(strAmount)
End If
' Add new record to temporary recordset
With rcdDNE
.AddNew
.Fields![RTN] = strRoutingNbr
.Fields![AccountNbr] = strAcct
.Fields![IndividualName] = strIndividualName
.Fields![Amount] = curAmount
.Update
End With
Loop
Close #intFileNbr
frmDNELoad.lblStatus.Caption = "Formatting Names..."
frmDNELoad.Refresh
DoEvents
' Parse the IndividualName field
rcdDNE.MoveFirst
Do Until rcdDNE.EOF
lngMidInitPos = 0
lngParsePos1 = 0
lngParsePos2 = 0
lngParsePos3 = 0
lngParsePos4 = 0
lngParsePos5 = 0
lngParsePos6 = 0
strParseString = ""
strParseFirstNm = ""
strParseMidInit = ""
strParseLastNam = ""
strParseString = Trim(rcdDNE.Fields![IndividualName])
' Replace double spaces (" ") with a single space (" ")
lngPos = InStr(1, strParseString, " ")
Do While lngPos
strParseString = Mid(strParseString, 1, lngPos - 1) & Mid(strParseString, lngPos + 1, Len(strParseString))
lngPos = InStr(1, strParseString, " ")
Loop
' Locate positions of remaining spaces
lngParsePos1 = InStr(1, strParseString, " ")
If lngParsePos1 = 0 Then
lngParsePos2 = 0
Else
lngParsePos2 = InStr(lngParsePos1 + 1, strParseString, " ")
End If
If lngParsePos2 = 0 Then
lngParsePos3 = 0
Else
lngParsePos3 = InStr(lngParsePos2 + 1, strParseString, " ")
End If
If lngParsePos3 = 0 Then
lngParsePos4 = 0
Else
lngParsePos4 = InStr(lngParsePos3 + 1, strParseString, " ")
End If
If lngParsePos4 = 0 Then
lngParsePos5 = 0
Else
lngParsePos5 = InStr(lngParsePos4 + 1, strParseString, " ")
End If
If lngParsePos5 = 0 Then
lngParsePos6 = 0
Else
lngParsePos6 = InStr(lngParsePos5 + 1, strParseString, " ")
End If
' Determine if Middle initial is present
If (lngParsePos3 - lngParsePos2) = 2 Then
lngMidInitPos = lngParsePos2 + 1
rcdDNE.Fields![MiddleName] = Mid(strParseString, lngMidInitPos, 1)
ElseIf (lngParsePos4 - lngParsePos3) = 2 Then
lngMidInitPos = lngParsePos3 + 1
rcdDNE.Fields![MiddleName] = Mid(strParseString, lngMidInitPos, 1)
ElseIf (lngParsePos5 - lngParsePos4) = 2 Then
lngMidInitPos = lngParsePos4 + 1
rcdDNE.Fields![MiddleName] = Mid(strParseString, lngMidInitPos, 1)
ElseIf (lngParsePos6 - lngParsePos5) = 2 Then
lngMidInitPos = lngParsePos5 + 1
rcdDNE.Fields![MiddleName] = Mid(strParseString, lngMidInitPos, 1)
ElseIf (lngParsePos2 - lngParsePos1) = 2 Then
lngMidInitPos = lngParsePos1 + 1
rcdDNE.Fields![MiddleName] = Mid(strParseString, lngMidInitPos, 1)
End If
' If there is a middle initial, everything to the left of it goes into the
' first name field, and everything to the right of it goes into the last
' name field. If there is no middle initial, everything after the first space
' goes into the last name field.
If lngMidInitPos <> 0 Then
rcdDNE.Fields![FirstName] = Trim(Left(strParseString, lngMidInitPos - 1))
rcdDNE.Fields![LastName] = Trim(Mid(strParseString, lngMidInitPos + 1, Len(strParseString)))
Else
rcdDNE.Fields![FirstName] = Trim(Left(strParseString, lngParsePos1))
rcdDNE.Fields![LastName] = Trim(Mid(strParseString, lngParsePos1 + 1, Len(strParseString)))
End If
rcdDNE.Update
rcdDNE.MoveNext
Loop
' Write records to Database
Call FindServerConnection_NoMsg
Dim cmdCommand As New ADODB.Command
If rcdDNE.EOF = False Then
rcdDNE.MoveFirst
cmdCommand.CommandText = "insert into DneFrc (RTN, AccountNbr, FirstName, MiddleName, LastName, Amount) values (RTN, AccountNbr, FirstName, MiddleName, LastName, Amount)"
cmdCommand.Execute()
rcdDNE.MoveNext
Loop Until rcdDNE.EOF = True
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您指出错误发生在 cmdCommand.Execute() 中,
这只是执行 CommandText 的值。
它确实与前面的代码无关,可以在示例中用所使用的数据的简单表以及对表的预期状态和结果的简单解释来替换它
原始的 CommandText 显然是错误的,因为值列表包含未声明的变量。
除非您使用“with rcdDNE”阻止代码,否则更改后的值列表将不起作用,以便您可以使用 .Fields 而不是 rcdDNE.Fields。
我将字符串剪切并粘贴到记事本中,以发现 ' 和 " 的混合,乍一看看起来不错,但我会检查这一点。
另外,请考虑数字和字符串值。
一个好的测试是简单地构建一个包含硬编码测试值的值列表。
您还可以将字段值分配给变量并在值列表中使用它们。这样,您可以在执行语句之前测试每个值的实际值的有效性。
您还可以尝试直接在 SQL 环境中执行命令,而不是每次都运行代码。
You state that the error is occurring in cmdCommand.Execute()
This is simply executing the value of CommandText.
It really has nothing to do with the previous code, which could be replaced in the example with a simple table of the data used, and a simple explanation of the expected states of the table(s), and the outcome
The original CommandText is obviously wrong because the values list comprises undeclared variables.
The changed values list won't work either unless you block the code using 'with rcdDNE' so you can use .Fields instead of rcdDNE.Fields.
I cut and pasted the string into notepad to discover the mix of ' and " which on first view look ok, except I would check this.
Also, think about the numeric and string values.
A good test would be to simply construct a value list with hard coded test values in it.
You could also assign the field values to variables and use these in your value list. This way you could test the actual value of each for validity prior to executing the statement.
You could also try executing the command directly in an SQL environment, rather than running your code each time.