ODBC Connection

jwbatey07

New Member
Joined
Apr 14, 2020
Messages
33
Office Version
  1. 365
Platform
  1. Windows
I've never attempted to connect to our SQL server using VBA. I do it all the time in Excel through the 'Data' tab. My goal is to write a VBA code that will query our database to find the status of a customer's account. ideally, I would use a comboxbox to lookup the customer by customer name or employee name, the data coming back would be their credit status. if the status is good it would allow our technician to make their product, if the status is "hold", it would not allow the form to come up. that's the goal. I found this code below that I think I can make work. I've removed the credentials for security purpose but I have check the connection by itself and it does connect. I have yet to write in the JOINS that I will be needing. At this point, I just want to be able to bring back the information from my database and put it into a worksheet. once I have the I can attempt to figure out the rest. when I run this code I get a syntax error on line

rs.Open mssql, oConn


Hopefully someone can help me out with this.


Sub DatatakenFromSQLServer()

Dim oConn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim fld As ADODB.Field
Dim mssql As String
Dim row As Integer
Dim Col As Integer
Dim ws As ThisWorkbook
Set ws = ThisWorkbook
Application.ScreenUpdating = False
Set oConn = New ADODB.Connection
Set rs = New ADODB.Recordset
mssql = "SELECT p21_view_contacts.id, p21_view_contacts.first_name, p21_view_contacts.last_name" & _
"FROM dbo.p21_view_contacts"
oConn.ConnectionString = "Driver={SQL Server}; Server=; Database=; VID=;PWD=" INFORMATION REMOVED FOR SECURITY PURPOSE

oConn.ConnectionTimeout = 30
oConn.Open
rs.Open mssql, oConn ERROR "INCORRECT SYNTAX NEAR"
If rs.EOF Then
MsgBox "No matching records found."
rs.Close
oConn.Close
Exit Sub
End If

row = 5
Col = 1
For Each fld In rs.Fields
Sheet2.Cells(row, Col).Value = fld.Name
Col = Col + 1
Next
rs.MoveFirst
row = row + 1
Do While Not rs.EOF
Col = 1
For Each fld In rs.Fields
Sheet2.Cells(row, Col).Value = fld
Col = Col + 1
Next
row = row + 1
rs.MoveNext
Loop
rs.Close
oConn.Close

End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
perhaps it's my SQL Statement. i've tried many variations, with the table name without the table name, with quotes. Here is an example of a statement I wrote for Bartender.


SELECT "p21_view_inv_tran"."document_no", "p21_view_inv_tran"."sub_document_no", "p21_view_inv_tran"."qty_allocated", "p21_view_inv_tran"."item_id", "p21_view_inv_tran"."trans_date", "p21_view_oe_hdr"."ship2_name", "p21_view_oe_hdr"."taker", "p21_view_oe_hdr"."po_no", "p21_view_address"."name", "p21_view_address"."id"
FROM "dbo"."p21_view_inv_tran"
INNER JOIN "dbo"."p21_view_oe_hdr" ON "dbo"."p21_view_inv_tran"."document_no" = "dbo"."p21_view_oe_hdr"."order_no" INNER JOIN "dbo"."p21_view_address" ON "dbo"."p21_view_oe_hdr"."carrier_id" = "dbo"."p21_view_address"."id"
WHERE sub_document_no
BETWEEN ?sub_document_no AND ?sub_document_no1
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top