which is a best connection in VB/VBA to connect the remote SQL server

R

rock

Hi all,

which is a best connection (in terms of speed and connectivity) in
VB/VBA to connect the remote SQL server.


Regards,
Shital
 
W

wwcoop

I'm not sure what you are asking specifically, but here is an example:

Set mstrSQLConnect to your connection path, i.e:
"Provider=SQLOLEDB;Data Source=SAI-SQL-SERVER;Initial
Catalog=NEWIFL;UID=SA;PWD=SYSADMIN"

Also you would need another sub "CriticalError" in the example given.

Basically, you just create your objects, make a connection, execute
your query and then destroy your objects and connection.

You probably will have many very similar functions like this which you
should put in their own module, modSQL for example.


*********************************
Public Function GetReportsList() As String

On Error GoTo DBEr

Dim strResults As String

Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim cmd As New ADODB.Command

Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
Set cmd = New ADODB.Command

cn.Open mstrSQLConnect
cmd.ActiveConnection = cn
cmd.CommandType = adCmdText
cmd.CommandText = "SELECT ReportNo, Rev FROM IFLReport"

With rs
.CursorType = adOpenForwardOnly
.CursorLocation = adUseClient
.LockType = adLockOptimistic
.Open cmd
End With

strResults = rs.GetString(, , ",", ",")

rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing

GetReportsList = strResults

Exit Function

DBEr:
MsgBox "Database error in function GetReportsList", vbCritical
CriticalError

End Function
*********************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top