Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you code in Visual Basic writing to an access database? 2

Status
Not open for further replies.

gwward

Programmer
Dec 5, 2003
11
0
0
US
I currently am writing information captured on a form to
a text file which I am importing into Access with VB. I now want to write the information directly to access. What are the steps invloved in doing so. Anyone's help would be
greatly appreciated!!!

Thanks,

Bill
b.ward@vanderbilt.edu
 
There are a couple of ways to do it. I suggest you take a look at ADO and DAO. A simple example:

Database Table: myTable(Field1 text, Field2 text)

put two textBoxes and a button on a form and add this:

option Explicit
'You will need a reference to Microsoft ActiveX Data Objects 2.5
Private Sub Command1_Click()
dim con as ADODB.Connection
dim connectionString as String

set con = New ADODB.Connection

connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=dbName.mdb"

con.CursorLocation = adUseClient
con.Open connectionString

con.execute "INSERT INTO myTable VALUES ('" & Text1 & "','" & Text2 & "')"
end sub


run the program, enter a word in each textBox and click on the button. Open the db from Access now and check if the words have been inserted in the table.
 
hi Mr. gwward,

either you can follow mr. nicsin which is also one of the effective methods of programming..

else you can use another effective method which is more simple for your application...

the coding is as follows :

Go and add the Microsoft DAO access library of any version from the references of your project menu..

consider my database name is MYDATA, my tablename is MYTABLE and my fieldname is MYTEXT.. then....


in GENERAL DECLARATIONS code as

dim db as database
dim rs as recordset

in FORM LOAD code as

set db = opendatabase(app.path(or any path) & "\MYDATA")

set rs = db.openrecordset("select * from (tablename), dbopendynaset)

in SAVE CLICK code as


set rs = db.openrecordset("select * from MYTABLE, dbopendynaset)

with rs

.addnew
rs.fields("MYTEXT") = text1.text
.update
end with


Now run your application, and check for the existence of the text in your table..

with regards,
manoj

 
Thank you very much that was a big help.

Bill WArd
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top