Posted by: Syed Sumair Zafar on: March 3, 2008
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”
….
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_EHoConn.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.OpenDataOpen = True
Exit Function
Open_EH:
‘ Call ErrorHandler(goConn)
‘DataOpen = False
‘Exit Function
End FunctionPublic 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
now in form2 on “connect to database” button event paste this code
Private Sub Command1_Click()
Dim boolPerform As BooleanScreen.MousePointer = vbHourglass
‘ Open the Database Engine
boolPerform = DataOpen(goConn)If boolPerform Then
Form1.Show
End IfScreen.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 StringstrSQL = “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.CloseEnd 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…
Hi.. i have a big problem.can u please help me how to retrieve data from Sql server 2005 using VB application. i reaaly need a full explaination hoew to do that because i stilll new at VB6.
Thank you
I am trying to retrieve data from Access 2007 Database and i am using following code in vb 2008
Dim Conn1 As New System.Data.OleDb.OleDbConnection(”Provider=Microsoft.Jet.OLEDB.4.0;Data Source=o:\VB\database\coffee\orders.mdb”)
‘create the SQL select statement
Dim output As New System.Data.OleDb.OleDbCommand(”SELECT * FROM Orders1″, Conn1)
‘open the connection
Conn1.Open()
‘ run the SQL query
lblBill.Text = output.ToString
output.ExecuteNonQuery()
‘close the connection
Conn1.Close()
Its not displaying anything from database instead its showing
System.Data.OleDb.OleDbCommand
How can i resilve it.
Thanks
Gibran
July 14, 2008 at 8:23 am
Hello dear
I am Useing VB6.0 on that I want to Fine the Libraray of Stetment
(App.path) Please Kindly Send me the Libraray name and the Method of that I will Be really thanks full
All the Best.
Bye