Visual Basic/ADODB

Advertisement


Question
Sir,
I'am a primary teacher teaching in STD III and highly interested in programming since I want to study more and I'am 50 years old. I went to study vb and dropped since I could not follow the speed of my teacher. Then I acquired something on vb. It lacks the guidence.
I read your answer to a question. The story of a newly married girl. I have been trying to connect ado to an access database. I made it and can manipulate on the table. Still I don't know the difference between adodc1 and adodb. And I want to know from where a tutorial describing all this is available. Hope u can suggest one. Thanking You
Mohanan V

Answer
what is the story of a newly married girl? i am curious, as i don't remember my answer. there are lots of tutorials in the web for adodb.
here is a sample code:
the declarations:
' check Project_references_Microsoft ActiveX Data objects 2.8library '(or higher)

Dim cn as Adodb.connection, rec as Adodb.recordset

'under form Load (or  in a module  with Public instead of Dim)
set cn = new ADODB.Connection
set rec=new ADODB.Recordset.

cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb;Persist Security Info=False"

' change the path to your database and the version of Jet.oledb
(you can get the path  using a ADODC and point it to your database, and copy the connectionstring and substitue in the above line.)

cn.Open

' your code for getting the  records requires change.

see a sample codesheet:  study this and make changes to suit your needs.

'you can addnew and display in  the same form1 and eliminate Form2.
'have 4 commandbuttons and 2 textboxes and add this code. you need to alter the path and add more textboxes and fields

Option Explicit
Dim conn As ADODB.Connection, rec As ADODB.Recordset
Dim esql As String, esql2 As String, searchvar As String
Private Sub Command1_Click()
Text1 = ""
Text2 = ""
Text3 = ""
Command4.Visible = True
Command1.Visible = False
Text1.SetFocus
End Sub

Private Sub Command2_Click()
If Not rec.EOF Then
rec.MoveNext
Else
rec.MoveLast
End If
GetText
End Sub

Private Sub Command3_Click()
If Not rec.BOF Then
rec.MovePrevious
Else
rec.MoveFirst
End If
GetText
End Sub

Private Sub Command4_Click()
On Error GoTo 1
If Text1 = "" Or Text2 = "" Then
Command4.Visible = False
Command1.Visible = True
Exit Sub
End If
rec.AddNew
rec.Fields(0) = Text1
rec.Fields(1) = Text2
rec.Fields(2) = Text3
rec.Update
If Not rec.EOF Then rec.MoveNext
rec.MoveFirst
GetText
Command4.Visible = False
Command1.Visible = True
Exit Sub
1
MsgBox ("duplicate value") & Text3
End Sub

'your code  to find  suits access form and  NOT vb form. i suppose you are in Visual Basic Project getting info from access table. for thst use

Private Sub Command5_Click()
Text1 = ""
Text2 = ""
Text3 = ""
searchvar = InputBox("enter item to find")
rec.Close
rec.Open ("select * from TestRavi where First=" & "'" & searchvar & "'"), conn, adOpenStatic, adLockReadOnly
  If rec.Fields(0) <> "" Then
  Text1 = rec.Fields(0)
  Text2 = rec.Fields(1)
Text3 = rec.Fields(2)
Else
MsgBox ("No matching records found")
rec.Close
rec.Open ("select * from testravi"), conn, adOpenDynamic, adLockOptimistic
GetText
End If
End Sub
'for integers use
'Dim searchvar2 As Integer
'searchvar2 = InputBox("enter Number")
'rec.Open ("select * from TestRavi where First=" & searchvar2), conn, adOpenStatic, adLockReadOnly
Private Sub Form_Load()
Set conn = New ADODB.Connection
Set rec = New ADODB.Recordset

conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\YOURDATABASENAME.mdb;Persist Security Info=False"
'ENTER CORRECT PATH HERE.
conn.Open
esql = "select * from TestRavi"  ' testravi is name of table
rec.Open (esql), conn, adOpenDynamic, adLockOptimistic
GetText
End Sub

Private Sub Form_Unload(Cancel As Integer)
rec.Close
conn.Close
Set conn = Nothing
End Sub
Private Sub GetText()
If rec.BOF = True Or rec.EOF = True Then Exit Sub
Text1 = rec.Fields(0)
Text2 = rec.Fields(1)
Text3 = ""
End Sub

CHANGES may be needed to suit your needs


Ravindra M.G.

Visual Basic

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ravindra

Expertise

visual basic application programming from design to access information, sql, engineering and commercial applications. access databases, excel.(VB6) basic questions in vb 2008. Optional: 1)a large number of people want me to do work which takes some time and effort. can do projects in vb 2008. Pl.note that i would like to be paid for such work. 2) if you want me to spend quality time and do special work, i expect to be paid a reasonable price for my time. 3) if you are pleased with my reply you could consider a good amount. 4) you can visit my website http://ravindra.coolpage.biz

Experience

programming since the past 15 years. have completed a large number of projects in Industrial applications, finance,accounts, correspondence. excel macros etc

Education/Credentials
engineering, management degrees.

Past/Present Clients
project work for a Norway company,USA company and a Canadian company completed. Freelance Project work and Teaching. helping programmers with their work
teaching vb

©2016 About.com. All rights reserved.