Connecting Vba to MS SQL (return & display value)

Forestq

Active Member
Joined
May 9, 2010
Messages
482
Hi

I'm doing simple connection to MS SQL and want to get count and display into excel:
Code:
Sub test()
    Dim conn As ADODB.Connection
    Dim rcrds As ADODB.Recordset
    Dim iRowNo, i As Long
    Dim id, name, value As String

    Dim wb As Workbook
    Dim ws As Worksheet

    Set wb = ActiveWorkbook
    Set ws = wb.Worksheets("Sheet1")

    Dim login,passwd,host,dbname  As String
    
    login = ""
    passwd = ""
    host = ""
    dbname = ""
  
    Set conn = New ADODB.Connection
    
    With conn
        .ConnectionString = "driver={sql server};Server=" + host + _
                            ";Database=" + dbname + _
                            ";User ID=" + login + _
                            ";Password=" + passwd + _
                            ";Trusted_Connection=False;"
        .Open
    End With
    
   Dim SQLstr As String
   SQLstr = "SELECT count(*) FROM table1"

   'Set rcrds = conn.Execute(SQLstr)

    ws.Range("K3").Vaue = 'how to display here count number????

   rcrds.Close
   conn.Close
   Set rcrds = Nothing
End Sub

How to display into Range("K3") count number?
 
Last edited:

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
thanks!I have already did something like:
Code:
Dim SQLstr As String   SQLstr = "SELECT count(*) FROM t1"   Set rcrds = conn.Execute(SQLstr)      Do While Not rcrds.EOF        ws.Range("E8").CopyFromRecordset rcrds  Loop
 
Last edited:
Upvote 0
You don't really need CopyFromRecordset when you only have one value:
Code:
    ws.Range("K3").Value = rcrds(0)
 
Last edited:
Upvote 0
when I did just as you wrote:

Code:
SQLstr = "SELECT count(*) FROM t1"
Set rcrds = conn.Execute(SQLstr)
ws.Range("E8").Vaue = rcrds(0)

I got error: Run time error - 438
object doesn't support this property or method
 
Upvote 0
I didn't spot the typo in your original code - it's Value, not Vaue. I'll correct my post.
 
Upvote 0

Forum statistics

Threads
1,214,926
Messages
6,122,305
Members
449,079
Latest member
juggernaut24

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