将CSV记录与MSAccess表中的记录进行比较

时间:2016-02-04 21:53:09

标签: vba ms-access access-vba

我的功能应该采用给定的表格,数字字段,电话公司字段,并查找是否有任何电话公司更改了数据库,然后将数据库的电话公司设置为新上传的电话公司。

虽然我在MSAccess中使用带有表格的表格对其进行了测试,但它确实记录了他们没有匹配的记录,然后相应地更新它们。

但是,它不适用于表格和导入的CSV文件。弹出没有错误,但主要是当我用消息框测试输出时,它似乎认为" ATT"和" ATT"不相等。 CSV文件中是否有一些不可见的空格/制表符?

Dim rs As DAO.Recordset
Dim sr As DAO.Recordset

'Open Billingcsv
Set rs = CurrentDb.OpenRecordset("Select [" & NumberName & "], [" & PhoneCompanyName & "] from [" & TableName & "] Where [" & _
NumberName & "] is not null ")

'Set tblPhoneDept
Set sr = CurrentDb.OpenRecordset("tblPhoneDept", dbOpenTable)

sr.Index = "PrimaryKey"
If (rs.RecordCount <> 0) Then
        rs.MoveFirst
        Do While rs.EOF <> True
            'check if phone company matches
            sr.Seek "=", rs.Fields("[" & NumberName & "]")
            If Not sr.NoMatch Then
                If sr![PhoneCompany] <> rs.Fields("[" & PhoneCompanyName & "]") Then
                    MsgBox sr![PhoneCompany] & " " & sr![Number] & " " & rs.Fields("[" & PhoneCompanyName & "]") & " " & rs.Fields("[" & NumberName & "]")
                    CurrentDb.Execute "Update [tblPhoneDept] SET [PhoneCompany] = " & rs.Fields("[" & PhoneCompanyName & "]") & _
                    " WHERE [number] = '" & rs.Fields("[" & NumberName & "]") & "'"
                    MsgBox sr![PhoneCompany] & " " & sr![Number] & " " & rs.Fields("[" & PhoneCompanyName & "]") & " " & rs.Fields("[" & NumberName & "]")
                Stop
                End If
            End If
            rs.MoveNext
        Loop
End If

CSV文件:

WirelessNumber PhoneCompany
(xxx) xxx-xxxx  ATT
(yyy) xxx-xxxx  ATT2
(zzz) xxx-xxxx  ATT3
(aaa) xxx-xxxx  Sprint
(bbb) xxx-xxxx  ATT2
(ccc) xxx-xxxx  ATT
(ddd) xxx-xxxx  ATT3

tblPhoneDept

Number PhoneCompany
(xxx) xxx-xxxx  ATT
(yyy) xxx-xxxx  ATT6 
(zzz) xxx-xxxx  ATT5
(aaa) xxx-xxxx  Sprint
(bbb) xxx-xxxx  ATT2
(ccc) xxx-xxxx  ATT
(ddd) xxx-xxxx  ATT3

2 个答案:

答案 0 :(得分:1)

将您的CSV文件链接为Excel外部链接表,在两个表之间创建连接(如果TableName是参数,则创建SQL(您将在此站点上找到好的帖子),然后对所有表运行更新查询选定的记录。

答案 1 :(得分:0)

最后,我最终将CSV文件导入其表格(tblATTPhonesBilling),删除了CSV文件,然后传递日期以帮助过滤最新的CSV条目。

从“导入CSV”部分传递这样的变量:

CheckChangedPhoneCompanies“tblATTPhonesBilling”,“无线号码”,“账单账号”,“市场周期结束日期”,MarketCycleDateStr

代码已更改为仅过滤它:

Public Function CheckChangedPhoneCompanies(TableName As String, NumberName As String, PhoneCompanyName As String, BillingCycleDateName As String, BillingCycleDate As String)
    Dim rs As DAO.Recordset
    Dim sr As DAO.Recordset

    'Open Billingcsv
    Set rs = CurrentDb.OpenRecordset("Select [" & NumberName & "], [" & PhoneCompanyName & "] from [" & TableName & "] Where [" & _
    NumberName & "] is not null AND [" & BillingCycleDateName & "] = #" & BillingCycleDate & "# ")

    'Set tblPhoneDept
    Set sr = CurrentDb.OpenRecordset("tblPhoneDept", dbOpenTable)

    sr.Index = "PrimaryKey"
    If (rs.RecordCount <> 0) Then
            rs.MoveFirst
            Do While rs.EOF <> True
                'check if phone company matches
                sr.Seek "=", rs.Fields("[" & NumberName & "]")
                If Not sr.NoMatch Then
                    If sr![PhoneCompany] <> rs.Fields("[" & PhoneCompanyName & "]") Then
                        MsgBox sr![PhoneCompany] & " " & sr![Number] & " " & rs.Fields("[" & PhoneCompanyName & "]") & " " & rs.Fields("[" & NumberName & "]")
                        CurrentDb.Execute "Update [tblPhoneDept] SET [PhoneCompany] = " & rs.Fields("[" & PhoneCompanyName & "]") & _
                        " WHERE [number] = '" & rs.Fields("[" & NumberName & "]") & "'"
                        MsgBox sr![PhoneCompany] & " " & sr![Number] & " " & rs.Fields("[" & PhoneCompanyName & "]") & " " & rs.Fields("[" & NumberName & "]")
                    Stop
                    End If
                End If
                rs.MoveNext
            Loop
    End If
End Function

我仍然不知道为什么直接与CSV文件比较时它不起作用,但这符合我的需要。