Click here to Skip to main content
15,904,926 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I need to connect from excel vba to Sql server 2008 r2. I have written a function in excel module but I get a False in excel cell However the mesgbox writtens the value 1
But in the front end excel when I call this function =ConnectDB("Server","DB","Sa","xxx") it returns the value false




VB
Function ConnectToDB(Server As String, Database As String, User As String, Pwd As String) As Boolean
 
    Set Conn = New ADODB.Connection
    Set objRst = New ADODB.Recordset
    On Error Resume Next
Conn.ConnectionString = "Provider=SQLOLEDB.1;Server=" & Server & "; Database=" & Database & ";User ID=" & User & ";Password=" & Pwd & ";"
Conn.Open
Msgbox(Conn.State)

    End Function


What I have tried:

I tried Declaring the Conn has Dim variables still by declaring Option Explicit.
Tried Debugging the values are passed correctly from the front end (i.e from excelvalue to function parameters)
Posted
Updated 2-Jun-16 2:53am
v3

1 solution

This function will always return false because you are not returning any value from it.

Try returning True if the State is 'Connected'

[EDIT] - To check if the State is connected use the following
VB
If (Conn.State And adStateOpen) = adStateOpen Then
    Return True
Else
    Return False
End If

[EDIT] OP still struggling...
Part of the problem here appears to be the connection string to the database. It looks like a mixture of OLEDB and ODBC. If possible use Windows Authentication as recommended by MS. But the connection string needs tidying up anyway.

Apologies for misleading you above with Return True - I forgot that VBA struggles to get up to even the dizzy heights of VB6 syntax. The following function works - note I've given two versions of the connection string
VB
Function ConnectToDB(Server As String, Database As String, User As String, Pwd As String) As Boolean

    Set Conn = New ADODB.Connection
    On Error Resume Next
    
    'This version if you have user id and password
    Conn.ConnectionString = "Provider=SQLOLEDB;Data Source=" & Server & "; Initial Catalog=" & Database & "; UserId=" & User & "; Password=" & Pwd & ";"
    'This way for the recommended approach of using Windows authentication
    Conn.ConnectionString = "Provider=SQLOLEDB;Data Source=" & Server & ";Initial Catalog=" & Database & ";Integrated Security=SSPI;"
    
    Conn.Open
    If (Conn.State And adStateOpen) = adStateOpen Then
        ConnectToDB = True
    Else
        ConnectToDB = False
    End If
 
End Function
 
Share this answer
 
v3
Comments
ShaHam11 2-Jun-16 11:24am    
I just added these code to check

If conn.State = True Then
MsgBox "Pass"
Else
MsgBox "F"
End If

"F" was printed ..:-(
CHill60 2-Jun-16 12:26pm    
Well it would print "F" because State will never equal True - that is not how it works - State Property (ADO)[^]
I've updated my answer with an example - note this is untested
ShaHam11 2-Jun-16 23:57pm    
Thanks for the sample with example ...Return True had some syntax issue. I tried replacing it with msgbox but still landed up with False.. Let me give you my requirement if you can suggest how to proceed with it... I

I wanted to write my own user defined function in excel for SQl executing SQL query from Excel something like this =SQLQuery("My SQL Query")... As a first step I was trying to connect to the db..
CHill60 3-Jun-16 4:10am    
"Some syntax issue" ...be specific - what syntax issue? Don't replace returning a value from a function with a message box
CHill60 3-Jun-16 5:35am    
I've updated my answer now I've got access to Excel

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900