2014-10-17 152 views
-1

我有2个项目列表,1个是客户端ID,另一个是国家保险号,并且被要求创建一个聪明的SUB,它将检查ID和NI的数据库,以及if theres结果复制数据,如果没有结果尝试ID,如果没有运气与ID尝试NI作为最后的手段 我已经试过这与下面的代码,开始查询基于当前单元格ID和NI查找信息,如果结果复制到单元格和完成,如果没有结果做相同的查询,但只是与ID如果结果复制其他尝试NI等。VBA在嵌套的条件语句中更改SQL语句

我不断收到记录集错误,不允许它被用于打开,但ive添加在每种情况之后关闭,但仍然没有结果。

这种想法可行,如果是缺少什么来回我的代码为它做我想做什么做

Dim con As ADODB.Connection 
Dim rec As ADODB.Recordset 
Set con = New ADODB.Connection 
Set rec = New ADODB.Recordset 

Dim sql As String 
Dim client As String 
Dim NI As String 

Dim myRange As Range 
Dim myCell As Range 
Dim msgStr As Variant 
Dim f As New Details_Bar 

Set myRange = Range("A2:A502") 

Dim x As Integer 
x = 1 
pos = 2 

With con 
    .Provider = "MSDASQL" 
    .ConnectionString = "DSN=ukfast" 
    .Open 
End With 

' Loop Start 
For Each myCell In myRange 

client = myCell.text 
NI = Cells(myCell.Row, 2).text 

    ' First Look 
    sql = "SELECT id,firstname,lastname,national_insurance FROM crm_clients WHERE id = '" & client & "' AND national_insurance = '" & NI & "' GROUP BY id" 

    rec.Open sql, con 
    ' If Record Found Auto Enter Data in cells 
    If Not (rec.BOF And rec.EOF) Then 

    Cells(myCell.Row, 7).CopyFromRecordset rec 

    rec.Close 

    MSG1 = MsgBox("Data Was Automatically Found", vbOKOnly) 

    'Else Begin to query the database again based on the Client ID 

    Else 

     sql = "SELECT id,firstname,lastname,national_insurance FROM crm_clients WHERE id = '" & client & "' GROUP BY id" 

     rec.Open sql, con 

     'If no result on ID then try NI Number 

      If (rec.BOF And rec.EOF) Then 

       sql = "SELECT id,firstname,lastname,national_insurance FROM crm_clients WHERE national_insurance = '" & NI & "' GROUP BY id" 

       rec.Open sql, con 

       Cells(myCell.Row, 100).CopyFromRecordset rec 

       MSG1 = MsgBox("The Following Data Was Found About Client based on NI" & x & vbNewLine & "Client ID : " & Cells(myCell.Row, 100) & vbNewLine & "First Name : " & Cells(myCell.Row, 101) & vbNewLine & "Last Name : " & Cells(myCell.Row, 102) & vbNewLine & "National Insurance : " & Cells(myCell.Row, 103) & vbNewLine & "Is this correct?", vbYesNoCancel, "Data Check") 

       Select Case MSG1 

       Case vbYes 
        Cells(myCell.Row, "G").Value = Cells(myCell.Row, 100) 
        Cells(myCell.Row, "H").Value = Cells(myCell.Row, 101) 
        Cells(myCell.Row, "I").Value = Cells(myCell.Row, 102) 
        Cells(myCell.Row, "J").Value = Cells(myCell.Row, 103) 
       Case vbNo 
        Details_Bar.Show 
       Case vbCancel 
        Exit For 
        Exit Sub 

       End Select 
       rec.Close 
      End If 

     Cells(myCell.Row, 100).CopyFromRecordset rec 

     MSG1 = MsgBox("The Following Data Was Found About Client based on ID" & x & vbNewLine & "Client ID : " & Cells(myCell.Row, 100) & vbNewLine & "First Name : " & Cells(myCell.Row, 101) & vbNewLine & "Last Name : " & Cells(myCell.Row, 102) & vbNewLine & "National Insurance : " & Cells(myCell.Row, 103) & vbNewLine & "Is this correct?", vbYesNoCancel, "Data Check") 

      Select Case MSG1 

      Case vbYes 
       Cells(myCell.Row, "G").Value = Cells(myCell.Row, 100) 
       Cells(myCell.Row, "H").Value = Cells(myCell.Row, 101) 
       Cells(myCell.Row, "I").Value = Cells(myCell.Row, 102) 
       Cells(myCell.Row, "J").Value = Cells(myCell.Row, 103) 
      Case vbNo 
       Details_Bar.Show 
      Case vbCancel 
       Exit For 
       Exit Sub 

      End Select 
     rec.Close 
End If 


' Update Vars 
pos = pos + 1 
x = x + 1 
'End Of Loop 
Next myCell 

GUI.CommandButton13.Enabled = False 
GUI.CommandButton15.Enabled = False 
Range("CA502:CZ502").Select 
Selection.Delete 
Range("A1").Select 

回答

1

想出解决方案。

就像我之前的一些工作一样,我意识到记录需要在循环结束之前始终关闭,并且每条不同的条件语句都必须在每个条件语句的开始处关闭,并立即在新查询后重新打开,然后在所有条件语句之外关闭

' First Look 
    sql = "SELECT id,firstname,lastname,national_insurance FROM crm_clients WHERE id = '" & client & "' AND national_insurance = '" & NI & "' GROUP BY id" 

    rec.Open sql, con 
    ' If Record Found Auto Enter Data in cells 
    If Not (rec.BOF And rec.EOF) Then 

    Cells(myCell.Row, 7).CopyFromRecordset rec 

    rec.Close '<--------------- Previous location 

    MSG1 = MsgBox("Data Was Automatically Found", vbOKOnly) 

    'Else Begin to query the database again based on the Client ID 

    Else 
     rec.close <-------------- New location 

     sql = "SELECT id,firstname,lastname,national_insurance FROM crm_clients WHERE id = '" & client & "' GROUP BY id" 

     rec.Open sql, con