Hi there, I was wondering if any one could help.
I'm new to classes but I want to create a class which holds information about my database connection.
I've created a class called CONNECT. In my class I have the following code.
<vb>
Option Explicit
Private m_strServer As String
Private m_strDatabase As String
Private m_strUID As String
Private m_strPwd As String
Public con As ADODB.Connection
Public rs As ADODB.Recordset
Property Let Server(ByVal newvalue As String)
m_strServer = newvalue
End Property
Property Get Server() As String
Server = m_strServer
End Property
Property Let Database(ByVal newvalue As String)
m_strDatabase = newvalue
End Property
Property Get Database() As String
Database = m_strDatabase
End Property
Property Let UserId(ByVal newvalue As String)
m_strUID = newvalue
End Property
Property Get UserId() As String
UserId = m_strUID
End Property
Property Let PassWord(ByVal newvalue As String)
m_strPwd = newvalue
End Property
Property Get PassWord() As String
PassWord = m_strPwd
End Property
Public Function Connect(ByVal strServer As String, ByVal strDB As String, _
ByVal strUID As String, ByVal strPwd As String, ByVal strQuery As String)
Set con = New ADODB.Connection
Set rs = New ADODB.Recordset
con.Open "Driver={SQL Server};Server=" & strServer & ";Database=" & strDB & ";Uid=" & strUID & ";Pwd=" & strPwd & ";"
rs.Open strQuery, con
End Function
Public Function DisConnect()
rs.Close
con.Close
Set rs = Nothing
Set con = Nothing
End Function
</vb>
This class holds all the information about connecting to the database and opens a record set.
However, in a normal module I have the following code:
<vb>
Public Sub UserForm_Initialize()
Dim myField As Variant, cmdText As String
Set oCONNECT = New CONNECT
Let oTramps.Database = "Test"
Let oTramps.UserId = "Cool"
Let oTramps.PassWord = "pa55word"
Let oTramps.Server = "Hind"
'Prepare command string
cmdText = "SELECT DISTINCT REF FROM football"
oCONNECT.Connect oCONNECT.Server, oCONNECT.Database, oCONNECT.UserId, oCONNECT.PassWord, cmdText
While Not rs.EOF
For Each myField In rs.fields
With FrmArrReport.ComboRef
.AddItem myField
End With
rs.MoveNext 'Move to the next record
Next myField
Wend
End sub
</vb>
However, the macro debugs due to the line "While Not rs.EOF" where an object is required. Is there any way for the programm to remember the recordset for when the macro goes back into the normal program.
Any help would be greatly appreciated.
Thanks
Bob.
I'm new to classes but I want to create a class which holds information about my database connection.
I've created a class called CONNECT. In my class I have the following code.
<vb>
Option Explicit
Private m_strServer As String
Private m_strDatabase As String
Private m_strUID As String
Private m_strPwd As String
Public con As ADODB.Connection
Public rs As ADODB.Recordset
Property Let Server(ByVal newvalue As String)
m_strServer = newvalue
End Property
Property Get Server() As String
Server = m_strServer
End Property
Property Let Database(ByVal newvalue As String)
m_strDatabase = newvalue
End Property
Property Get Database() As String
Database = m_strDatabase
End Property
Property Let UserId(ByVal newvalue As String)
m_strUID = newvalue
End Property
Property Get UserId() As String
UserId = m_strUID
End Property
Property Let PassWord(ByVal newvalue As String)
m_strPwd = newvalue
End Property
Property Get PassWord() As String
PassWord = m_strPwd
End Property
Public Function Connect(ByVal strServer As String, ByVal strDB As String, _
ByVal strUID As String, ByVal strPwd As String, ByVal strQuery As String)
Set con = New ADODB.Connection
Set rs = New ADODB.Recordset
con.Open "Driver={SQL Server};Server=" & strServer & ";Database=" & strDB & ";Uid=" & strUID & ";Pwd=" & strPwd & ";"
rs.Open strQuery, con
End Function
Public Function DisConnect()
rs.Close
con.Close
Set rs = Nothing
Set con = Nothing
End Function
</vb>
This class holds all the information about connecting to the database and opens a record set.
However, in a normal module I have the following code:
<vb>
Public Sub UserForm_Initialize()
Dim myField As Variant, cmdText As String
Set oCONNECT = New CONNECT
Let oTramps.Database = "Test"
Let oTramps.UserId = "Cool"
Let oTramps.PassWord = "pa55word"
Let oTramps.Server = "Hind"
'Prepare command string
cmdText = "SELECT DISTINCT REF FROM football"
oCONNECT.Connect oCONNECT.Server, oCONNECT.Database, oCONNECT.UserId, oCONNECT.PassWord, cmdText
While Not rs.EOF
For Each myField In rs.fields
With FrmArrReport.ComboRef
.AddItem myField
End With
rs.MoveNext 'Move to the next record
Next myField
Wend
End sub
</vb>
However, the macro debugs due to the line "While Not rs.EOF" where an object is required. Is there any way for the programm to remember the recordset for when the macro goes back into the normal program.
Any help would be greatly appreciated.
Thanks
Bob.