By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,646 Members | 2,105 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,646 IT Pros & Developers. It's quick & easy.

add items

lee123
100+
P: 556
this visual basic language is a tough cookie i don't understand how i would get to save a record in a form after you have entered it in.

i have made a access database (2000) that i want to add data in through a visual basic form. in my visual basic form i have fields like:

bill ID
Bill Name
Account Number
Reference Number

how do i add new data in this field using a command button that will save it and enter it in my database? do i need a Ado control in this form as well. or can i put code in the button to add the new data i enter in the fields because when i try to put code in the button there is no (add method) in the tool tips.
how do i do this?


lee123
Dec 23 '07 #1
Share this Question
Share on Google+
10 Replies


lee123
100+
P: 556
by the way im useing visual basic 6.0 i would of used a datacontrol but it don't like access 2000
Dec 23 '07 #2

vdraceil
100+
P: 236
this visual basic language is a tough cookie i don't understand how i would get to save a record ...
Assuming I got your question correctly.

Lee, you don't necessarily need a DAO or ADO control on your form. You can add records to a database by code also.

The following is a DAO code which I use.

Expand|Select|Wrap|Line Numbers
  1. dim db as database
  2. dim rs as recordset
  3.  
  4. Private Sub Form1_Load()
  5.   set db=opendatabse("\\yourfile.mdb")
  6.   set rs=db.openrecordset("tablename")
  7.   ...
  8. End Sub
  9.  
  10. Private Sub Command1_Click()
  11.   rs.addnew
  12.   rs.fields(0)=text1.text
  13.   rs.fields(1)=text2.text
  14.   ...
  15.   ...
  16.   rs.update 
  17. End Sub
Try this!
Experts will help you if you need to work with ADO.
Dec 23 '07 #3

debasisdas
Expert 5K+
P: 8,127
It's better to use ADO code instead of control.
  1. Select Microsoft Activex Data Objects 2.7 from project reference.
  2. Create a connection object
    Dim con As New ADODB.CONNECTION
  3. Open the connection
    con.Open "your connection string"
  4. con.Execute "insert into tablename(field1,field2,....) values (value1,value2,.......)"
Dec 23 '07 #4

lee123
100+
P: 556
Hi there dedasisdas,

I'm not understanding what you're doing here. I mean I did what you said in the project reference and checked the right box then I clicked on the adodc1 (to get the property window) and scrolled down to (connectionstring) and click on the elipse and chose this:

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\Microsoft Visual Studio\VB98\TrackYourBills.mdb;Persist Security Info=False

Maybe I did this wrong.
After that I kinda got lost as to what you're saying next.

dim con as new ADODB.CONNECTION

3.open the connection
con.open "your connection string"

4.con.execute "insert into tablename(field1,field2,....) values (value1,value2,.......)"
Please help me understand this.


Oh vdraciel,

Thanks for your response but I did yours and it did work. I assume this is for the datacontrol and not the adodc control (ado).
Dec 23 '07 #5

lotus18
100+
P: 866
I'm not understanding what you're doing here ...
Hi lee

You don't necessarily need an adodc control, what dedasisdas meant was from your Project menu > References > and checked Microsoft ActiveX Data Objects 2.8 Library.

For the connection:

Expand|Select|Wrap|Line Numbers
  1. Public con As ADODB.Connection
  2.  
  3. Public Sub SetConnection()
  4.     Dim dbPath$ 'String
  5.     dbPath = App.Path & "\Database\YourDatabase.mdb" 'Database path
  6.     Set con = New ADODB.Connection 'Instantiate new object 
  7.     con.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbPath & _
  8.     ";Persist Security Info=False" 'Open the connection
  9. End Sub
For adding new record
Expand|Select|Wrap|Line Numbers
  1. 'Assuming you are working with textboxes   
  2.  
  3. SetConnection 'Connection
  4. con.Execute "Insert Into TableName ([FieldName1],[FieldName2],[FieldNameN]) Values " _
  5.     & "('" & Text1.Text & "','" & Text2.Text & "','" & TextN.Text & "')" 
OR
Expand|Select|Wrap|Line Numbers
  1. SetConnection 'Connection
  2. Dim rs As New ADODB.Recordset
  3. rs.Open"Insert Into TableName ([FieldName1],[FieldName2],[FieldNameN]) Values " _
  4.     & "('" & Text1.Text & "','" & Text2.Text & "','" & TextN.Text & "')", con, 3, 3  
  5.  
Note: When you open any object you have to close it.
Rey Sean
Dec 24 '07 #6

lee123
100+
P: 556
Hi there lotus18,

Thanks for clearing that up. It's just VB is something else and I'm trying to learn (as we all are) how to do certain things to get the knowledge. You know?

I have a command button (Add New) that I wanted to use to add the information from the textboxes, and have it add the information I typed in the database I have made in Access (2000).

But let me try this code out the way you have done it, to see if I got this.

lee123
Dec 24 '07 #7

lotus18
100+
P: 866
Good luck. I'm sure you will get it right.


Rey Sean
Dec 24 '07 #8

debasisdas
Expert 5K+
P: 8,127
Hope lotus have solved your problem with the code.
Dec 24 '07 #9

lee123
100+
P: 556
he did. thanks,

lee123
Dec 24 '07 #10

lotus18
100+
P: 866
he did. thanks,

lee123
You're welcome :)


Rey Sean
Dec 25 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.