Check out PakStarted

while working in the lab on VB i got stuck so try to find the colution on net trust me it was not an easy task to find the desired solution so thought to share my work on my blog for other students so they can do “chaaapaaaaa” :D….

So i assume you know the basic….first create a module

Public goConn As New ADODB.Connection

Public Function DataOpen(oConn As Connection) As Boolean
‘On Error GoTo Open_EH

oConn.CursorLocation = adUseClient

‘ Set the connection string by calling
‘ a function.
oConn.ConnectionString = ConnectString()

‘ Set the mode of the connection
oConn.Mode = adModeReadWrite

‘ Open the Connection
oConn.Open

DataOpen = True

Exit Function

Open_EH:

‘ Call ErrorHandler(goConn)
‘DataOpen = False
‘Exit Function
End Function

Public Function ConnectString() As String
‘ SQL Server using an ODBC Data Source
‘ConnectString = “DSN=EmployeeSQLServer;” & _
‘ “UID=sa;PWD=;” & _
‘ “DATABASE=Employees”

‘ SQL Server using OLE DB Provider
‘ConnectString = “Provider = sqloledb;” & _
‘ “Data Source = (local);” & _
‘ “Initial Catalog = Employees;” & _
‘ “User Id = sa;” & _
‘ “Password = ; “

‘ Jet MDB
ConnectString = “Provider=Microsoft.Jet.OLEDB.3.51” & _
“;Data Source=” & App.Path & “..adoEmployees.mdb”
End Function

we are using Microsoft jet OLEDB to retrieve data which only work with Microsoft access database which is not as good as other ODBC and OLEDB …..

“GOCONN” is our connection…..

Now we will create two forms….one through which we connect to our database and second in which we show data..check the snaps for idea

form1 form2

now in form2 on “connect to database” button event paste this code

Private Sub Command1_Click()
Dim boolPerform As Boolean

Screen.MousePointer = vbHourglass

‘ Open the Database Engine
boolPerform = DataOpen(goConn)

If boolPerform Then
Form1.Show
End If

Screen.MousePointer = vbDefault

End Sub

and in form 1…paste this code on show event button

Private Sub Command1_Click()
Dim oRS As Recordset
Dim strSQL As String

strSQL = “SELECT ”
strSQL = strSQL & “szLast_nm, ”
strSQL = strSQL & “sFirst_nm ”
strSQL = strSQL & “FROM tblEmployees “

Set oRS = New Recordset
oRS.Open strSQL, goConn, _
adOpenForwardOnly, adLockReadOnly
Do Until oRS.EOF
lstNames.AddItem oRS(“sFirst_nm”) & _
” ” & oRS(“szLast_nm”)

oRS.MoveNext
Loop
oRS.Close

End Sub

ok the coding is done…. Also set your startup object to FORM2 by going in project menu bar then project properties……we will show form2 first to connect to data base if it could not connect to database it will show an error otherwise show form1..
.

Advertisements