Advertisement
Guest User

Untitled

a guest
Jul 25th, 2014
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. Sub NO6_Postcode()
  2.  
  3. 'Connection Variables
  4. Dim cn As ADODB.Connection
  5. Dim rs As ADODB.Recordset
  6. Dim cmd As ADODB.Command
  7. Set cn = New ADODB.Connection
  8. Set rs = New ADODB.Recordset
  9. Set cmd = New ADODB.Command
  10.  
  11. ' Ranges
  12. Dim myRange As range
  13. Dim nextUsed As range
  14. Dim sqlQry As String
  15. Dim myCell As range
  16. Set myRange = range("A1:A10")
  17. Set nextUsed = range("F1:F10")
  18.  
  19. 'connection to the database via DSN
  20. With cn
  21. .Provider = "MSDASQL"
  22. .ConnectionString = "DSN=localhostTest"
  23. .Open
  24. End With
  25.  
  26. For Each myCell In myRange
  27.  
  28. sqlQry = myCell.Text
  29.  
  30. Set cmd.ActiveConnection = cn
  31. cmd.CommandText = "SELECT * FROM test WHERE name LIKE '" & sqlQry & "' "
  32. cmd.CommandType = adCmdText
  33.  
  34. Set rs.Source = cmd
  35. rs.Open
  36.  
  37. ActiveSheet.range("F1").CopyFromRecordset rs
  38.  
  39. Next myCell
  40.  
  41. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement