我知道之前已经问过,但我看过的问题都有点不同。我正在创建一个临时表,导入数据,将数据放在现有表中,并删除临时表。这是我的代码:
Dim db As Database
Dim StrSql As String
Dim rst As Recordset
Set db = CurrentDb()
If DCount("[Name]", "MSysObjects", "[Name] = 'tblInfoLink'") = 1 Then
DoCmd.DeleteObject acTable, "tblInfoLink"
StrSql = "SELECT tblStaff.LastName, tblStaff.FirstName, tblStaff.Rank, tblStaff.PriPhone, " _
& " tblStaff.SecPhone, tblStaff.HireDate INTO tblInfoLink " _
& " FROM [\\Server\BE.accdb].tblStaff " _
& " WHERE (((tblInfo.LastName)=[tblInfo].[LastName]) AND ((tblInfo.FirstName)=[tblInfo].[FirstName]) AND ((tblInfo.HireDate)=[tblInfo].[HireDate]));"
db.Execute StrSql
ElseIf DCount("[Name]", "MSysObjects", "[Name] = 'tblInfoLink'") = 0 Then
StrSql = "SELECT tblStaff.LastName, tblStaff.FirstName, tblStaff.Rank, tblStaff.PriPhone, " _
& " tblStaff.SecPhone, tblStaff.HireDate INTO tblInfoLink " _
& " FROM [\\Server\BE.accdb].tblStaff " _
& " WHERE (((tblStaff.LastName)=[tblInfo].[LastName]) AND " _
& " ((tblStaff.FirstName)=[tblInfo].[FirstName]) AND ((tblStaff.HireDate)=[tblInfo].[HireDate]));"
db.Execute StrSql
End If
StrSql = "Update tblInfo Set PriPhone = Null, SecPhone = Null"
db.Execute StrSql
StrSql = "INSERT Into tblInfo (PriPhone, SecPhone) " _
& " SELECT tblInfoLink.PriPhone, tblInfoLink.SecPhone " _
& " FROM tblInfoLink " _
& " WHERE (((tblInfoLink.LastName)=[tblInfo].[LastName]) AND " _
& " ((tblInfoLink.FirstName)=[tblInfo].[FirstName]) AND ((tblInfoLink.HireDate)=[tblInfo].[HireDate]));"
db.Execute StrSql
'Delete Linked Tables
DoCmd.DeleteObject acTable, "tblInfoLink"
一旦我尝试运行它,我得到运行时错误'3061'参数太少。预期3.当我点击调试时,高亮度区域为:
db.Execute StrSql
我知道结束之后,一切正常。它实际上是最重要的部分,它不起作用。
有什么想法吗?提前谢谢!
答案 0 :(得分:0)
好的,我在过去几天做了一些修改。这就是我现在所处的位置。
Dim db As Database
Dim StrSql As String
Dim rst As Recordset
Set db = CurrentDb()
'Link to Database to retrieve Phone Numbers
If DCount("[Name]", "MSysObjects", "[Name] = 'tblInfoLink'") = 1 Then
DoCmd.DeleteObject acTable, "tblInfoLink"
StrSql = "SELECT ClockNbrId, PriPhone, SecPhone INTO tblInfoLink " _
& " FROM [\\Server\BE.accdb].tblStaff; "
Debug.Print StrSql
db.Execute StrSql
ElseIf DCount("[Name]", "MSysObjects", "[Name] = 'tblInfoLink'") = 0 Then
StrSql = "SELECT ClockNbrId, PriPhone, SecPhone INTO tblInfoLink " _
& " FROM [\\Server\BE.accdb].tblStaff; "
Debug.Print StrSql
db.Execute StrSql
End If
'Delete All Phone Numbers (Primary and Secondary) from tblInfo
StrSql = "Update tblInfo Set PriPhone = Null, SecPhone = Null"
db.Execute StrSql
'Update Phone Numbers of Current Staff
StrSql = "UPDATE tblInfo SET tblInfo.PriPhone = tblInfoLink.PriPhone" _
& " WHERE tblInfoLink.ClockNbrId = tblInfo.SSN ; "
Debug.Print StrSql
db.Execute StrSql
'Delete Linked Table
DoCmd.DeleteObject acTable, "tblInfoLink"
Exit_SyncStaff:
Set db = Nothing
基本上这就是我想要发生的事情。 我运行该命令,它使所有当前电话号码为空,连接到服务器上的数据库,收集所有电话号码(主要和次要)并将它们插入包含相同SSN的行。我能够开始使用SSN作为插入特定行所需的唯一标准,而不是名字,姓氏和雇用日期。我希望这是有道理的。
答案 1 :(得分:0)
只是为了让每个人都知道,我终于明白了。
Dim db As Database<br>
Dim StrSql As String<br>
Dim tblCount As Integer<br>
Const cstrPrompt As String = "Are you sure you want to start the import process? Yes/No"
If MsgBox(cstrPrompt, vbQuestion + vbYesNo) = vbYes Then
Set db = CurrentDb()`
'Link to Lt's Database to retrieve Phone Numbers
StrSql = "UPDATE tblInfo SET PriPhone = Null, SecPhone = Null;"
db.Execute StrSql
On Error Resume Next
DoCmd.DeleteObject acTable, "tblPhone"
StrSql = "CREATE TABLE tblPhone " _
& " (SSN CHAR, PriPhone CHAR (20), SecPhone CHAR(20));"
db.Execute StrSql
StrSql = "INSERT INTO tblPhone (SSN, PriPhone, SecPhone) " _
& " SELECT ClockNbrId, PriPhone, SecPhone " _
& " FROM [\\Server\BE.accdb].tblStaff; "
db.Execute StrSql
StrSql = "UPDATE tblInfo INNER JOIN tblPhone ON tblInfo.SSN = tblPhone.SSN" _
& " SET tblInfo.PriPhone = [tblPhone].[PriPhone], tblInfo.SecPhone = [tblPhone].[SecPhone] " _
& " WHERE (((tblInfo.SSN)=[tblPhone].[SSN])); "
db.Execute StrSql
DoCmd.DeleteObject acTable, "tblPhone"
Const cstrPrompt2 As String = "The import process is complete"
If MsgBox(cstrPrompt2, vbOKOnly, "Import Complete!") = vbOK Then
Me.ImportDel.Enabled = False
Me.ImportDel.Caption = "Import Complete"
Exit_SyncStaff:
Set db = Nothing
Else
Cancel = True
End If
End If`
谢谢大家的帮助。我想发布解决方案,所以如果将来有问题,其他人可以参考这个。似乎我的问题是INNER JOIN
。谢谢你的建议!