title: SQL query to extract employee details from access table
body: <p>I am new to Excel VBA. I have a user form in which I am trying to populate names of those employees who are AMO. I have a database called <code>Ofc</code>. Under which I have a table <code>EmployeeDetails</code>. Primary key is <code>PeoplesoftId</code>.</p>
  
  <p>Here is the structure and the contents of the <code>Employee</code> table:</p>
  
  <pre class="lang-none prettyprint-override"><code>PeoplesoftId  Nameofemployee  RacifId  Employeeid  Designation
  43243309      Maddala         V43309   99651823    AMO
  43243310      Abhishek        A43301   99651824    AMO
  43243311      Atanu           A43311   99651825    MO
  43243312      Rajiv           R43312   99651826    CSE
  </code></pre>
  
  <p>This is the code I've written so far:</p>
  
  <pre class="lang-vb prettyprint-override"><code>Dim cnn As ADODB.Connection 'dim the ADO collection class
  Dim rs As ADODB.Recordset 'dim the ADO recordset class
  Dim dbPath As String
  Dim SQL As String
  Dim i As Integer
  Dim var
  'add error handling
  On Error GoTo errHandler:
  'Disable screen flickering.
  Application.ScreenUpdating = False
  dbPath = "E:\office_hsbc\ofc.accdb"
  var = "AMO"
  Set cnn = New ADODB.Connection ' Initialise the collection class variable
  cnn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" &amp; dbPath
  cnn.Open
  SQL = "SELECT Nameofemployee FROM EmployeeDetails where Designation= '" &amp; var &amp; "'"
  Set rs = New ADODB.Recordset 'assign memory to the recordset
  rs.Open SQL, cnn
  If rs.EOF And rs.BOF Then
  rs.Close
  cnn.Close
  'clear memory
  Set rs = Nothing
  Set cnn = Nothing
  'Enable the screen.
  Application.ScreenUpdating = True
  MsgBox "There are no records in the recordset!", vbCritical, "No Records"
  Exit Sub
  End If
  For i = 0 To rs.Fields.Count - 1
      comboamo.AddItem rs.Fields(i).Value, i
  Next
  rs.Close
  cnn.Close
  Set rs = Nothing
  Set cnn = Nothing
  MsgBox "Congratulation the data has been successfully Imported", vbInformation, "Import successful"
  'error handler
  On Error GoTo 0
  Exit Sub
  errHandler:
  'clear memory
  Set rs = Nothing
  Set cnn = Nothing
  MsgBox "Error " &amp; Err.Number &amp; " (" &amp; Err.Description &amp; ") in procedure Import_Data"
  </code></pre>