So... I have to pull information from a database called sales orders. In excel, I need an input box to appear where the user inputs a customer ID, then the customer ID, First Name, Last Name, City, and State appear in Excel. When I run my code, it populates the column headers and nothing else. In other words, if I enter 7 in the input box it should pull this information for customer ID number & in the access database. I'm sure I'm just missing something small. I am an absolute newbie. Thanks in advance.
Here is my code:
Sub Test()
Dim RequestedID As String
RequestedID = Val(InputBox("Please Enter The Customer ID.", "Input"))
Dim cn As New ADODB.Connection
cn.ConnectionString = "Data Source=C:\Users\R01222430\OneDrive - University of Scranton\Documents\Sales Orders Homework\sales.accdb;"
cn.Provider = "Microsoft.ACE.OLEDB.12.0"
cn.Open
Dim rs As New ADODB.Recordset
Dim SQL As String
SQL = "Select CustomerID, CustFirstName, CustLastName, CustCity, CustState FROM Customers WHERE CustomerID = " & RequestedID & " "
rs.Open SQL, cn
Dim topCell As Range
Set topCell = Range("A1")
Sheet1.Cells.Clear
Dim rowCount As Integer
rowCount = 0
topCell.Offset(0, 0).Value = "Customer ID"
topCell.Offset(0, 1).Value = "First Name"
topCell.Offset(0, 2).Value = "Last Name"
topCell.Offset(0, 3).Value = "City"
topCell.Offset(0, 4).Value = "State"
Do While Not rs.EOF
rowCount = rowCount + 1
If CustomerID = rs.Fields("CustomerID") Then
topCell.Offset(rowCount, 0).Value = rs.Fields("CustomerID")
topCell.Offset(rowCount, 1).Value = rs.Fields("CustFirstName")
topCell.Offset(rowCount, 2).Value = rs.Fields("CustLastName")
topCell.Offset(rowCoaunt, 3).Value = rs.Fields("CustCity")
topCell.Offset(rowCount, 4).Value = rs.Fields("CustState")
Exit Do
End If
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub
Here is my code:
Sub Test()
Dim RequestedID As String
RequestedID = Val(InputBox("Please Enter The Customer ID.", "Input"))
Dim cn As New ADODB.Connection
cn.ConnectionString = "Data Source=C:\Users\R01222430\OneDrive - University of Scranton\Documents\Sales Orders Homework\sales.accdb;"
cn.Provider = "Microsoft.ACE.OLEDB.12.0"
cn.Open
Dim rs As New ADODB.Recordset
Dim SQL As String
SQL = "Select CustomerID, CustFirstName, CustLastName, CustCity, CustState FROM Customers WHERE CustomerID = " & RequestedID & " "
rs.Open SQL, cn
Dim topCell As Range
Set topCell = Range("A1")
Sheet1.Cells.Clear
Dim rowCount As Integer
rowCount = 0
topCell.Offset(0, 0).Value = "Customer ID"
topCell.Offset(0, 1).Value = "First Name"
topCell.Offset(0, 2).Value = "Last Name"
topCell.Offset(0, 3).Value = "City"
topCell.Offset(0, 4).Value = "State"
Do While Not rs.EOF
rowCount = rowCount + 1
If CustomerID = rs.Fields("CustomerID") Then
topCell.Offset(rowCount, 0).Value = rs.Fields("CustomerID")
topCell.Offset(rowCount, 1).Value = rs.Fields("CustFirstName")
topCell.Offset(rowCount, 2).Value = rs.Fields("CustLastName")
topCell.Offset(rowCoaunt, 3).Value = rs.Fields("CustCity")
topCell.Offset(rowCount, 4).Value = rs.Fields("CustState")
Exit Do
End If
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub