 |
(Pages: 1 2 ) |
 |
sumathige Level: Graduate
 Registered: 24-05-2005 Posts: 11
|
insert into MSAccess using recordset in VB
I am using MSAccess97 & VB6.
Created a DB using Visual Data Manager.
How to insert into DB usng the recordset from the forms.
Please reply
|
|
24-05-2005 at 09:54 AM |
|
|
yronium Level: Moderator

 Registered: 14-04-2002 Posts: 907
|
Re: insert into MSAccess using recordset in VB
quote: 1) The registration number(field in control1) present in Form1 should be updated in the form2.
Please notice that in my sample code I used a variable to store the last ID before adding a new record. You should: Open the recordset ==> Move on the last record ==> set the variable on the ID field value ==> add a new record ==> set the ID field as variable + 1.
Then in form2, you can execute a Requery method on the recordset. Remember that controls have a DataSource/DataField property, in order to be bound to a given recordset. If you have bound a control to a recordset, you just need to requery it and the control is automatically updated, otherwise you need to manually update the contol.quote: 2) When I select item in list box( registration number) in form2 a few data should be updated in the controls(textboxes) in form2.
The simpler way is open the form being based on a query, to be executed once the number has been selected. You can simply add a WHERE clause to your query.quote: 3)when an item is selected in a combobox,based on the selected item data should be populated in other listbox.how can it be done.
The same criteria as above: if the control is bound, just requery it, and the recordset will be reqeried; otherwise, reload all the values into it. You better put the listbox loading code into a separate Sub routine: when you load the form you call it, and once more when you need to update the values you call it again.
____________________________
Real Programmer can count up to 1024 on his fingers
|
|
26-05-2005 at 06:46 AM |
|
|
yronium Level: Moderator

 Registered: 14-04-2002 Posts: 907
|
Re: insert into MSAccess using recordset in VB
My dear "complete-programming-newbie!!", hello and welcome in the forum.
Are you sure you searched in the forum about your doubts? Here is only the latter post about connecting ADO-VB6, and notice there are some links in it that you can follow.
Connecting VB6 to a database by ADO is not a three-rows issue, and we could be more helpful if you'd post some code of what you've done at this time.
Anyway, in general words:
when you need to link a database to your project - say, filling your textboxes with database values - you can use ADO. (Or DAO, or RDO instead, or JRO, but these are other technologies. ADO is the most recent, and the most used.) So first, you should add ADO to your Reference window in the Project menu. "ADO" means "Microsoft ActiveX Data Objects 2.x Library"; once you checked it into that window, when you declare a variable into your code by writing "Dim x As ..." in the dropdown list of IntelliSense is avaliable the ADODB option, and with a dot you will be prompted with another type list. So you can declare "Dim cn As ADODB.Connection, rs As ADODB.Recordset..." etc.
Once you declared an ADODB.Recordset variable (a Recordset variable can contain a whole database table), you can link it to your external db file. In order to do this, you have to use a Connection object. The Connection object manages the "dialogue" between ADO and the db file. The Connection is tuned by setting all its parameters (such as the file name, the dataprovider name, etc.) by a ConnectionString. So, you build your ConnectionString and then open the connection (remember to close it and set it on Nothing when you're finished. And so the recordset object as well).
When the connection is open, you can use it to fill your recordset variable with the database value. The Recordset.Open method requires as an argument an open connection to use. And the Recordset object exposes an ActiveConnection property that says which connection the current recordset is open with. In fact you could have two or three connections open at one time, and be connected with two or three different db files, and use them all together to fill many recordsets with many tables or queries from the files.
Now you have a whole table stored int a recordset variable, and you have a form, with its textboxes waiting for data. You can set for each textbox its DataSource property on your recordset variable, and its DataField property on the name of a specific field from the recordset. So the textbox is bound to the value of this field, and when you move from one record to another, the value in the textbox changes. To navigate the recordset you can use one of the Recordset.Movexxxx methods (so, for instance, to move to the last record yo can write rs.MoveLast), and you can build your own recordset browser by a simple array of command buttons, and setting their Click events with the appropriate method.
This is the theory. I have only one thing more to say: you can easily do all these things if you add an Adodc control into your form. The Adodc control is a recordset browser that manages the whole recordset, so you can set all the textboxes' DataSource property on it. To have it avaliable in your control toolbar, you have to add a Microsoft ADO Data Control checkmark into the Project/Components window.
Well I hope is enough to start, and hope I have been clear. You didn't specify your level of complete-newbie-ness, so maybe you knew already many things I wrote down. You can find plenty of sample code here and along the web.
Hope it helps. Ask again and post your code if there's anything not so clear.
____________________________
Real Programmer can count up to 1024 on his fingers
|
|
14-06-2005 at 06:09 PM |
|
|
Ramya Level: Sage
 Registered: 08-04-2005 Posts: 60
|
Re: insert into MSAccess using recordset in VB
Hai Sumathi
R u from tamilnadu.WEll reg ur question to compare two dates.U can use the datediff() function.Check out for datediff() function in MSDN.If u need more guidance then please post here.
And i hope so if u have sound knowledge in VB and OOPS then its easy to migrate to .Net as i am doing it now.
|
|
15-06-2005 at 05:50 AM |
|
|
yronium Level: Moderator

 Registered: 14-04-2002 Posts: 907
|
Re: insert into MSAccess using recordset in VB
quote: lolly wrote:
Maybe it was the effect of a good nights sleep but i figured out what the problem was and it works!
I guess that "Teach yourself VB6 in 24 hrs" was meaning "...including sleeping time"
You succeeded! Applause! Now you have to plan when you want your code is executed.
Your code adds a new record on the database. If you place it all into the cmdConnect_Click event routine, the whole code is executed at every click on that particular button. So you should project some other buttons (say, routines) to e4xecute the various parts of code.
Usually the connection and the recordset are open in the Form_Load event, and they are set on Nothing and closed in the Form_Unload event. Then, every single operation is assigned to some buttons.
For instance, you could have a cmdNewRecord_Click event that simply disconnects and clears all the textboxes, a cmdSubmit_Click event that adds a new empty record to the recordset, fills recordset's fields with the textboxes values, Update the recordset and reconnect the textboxes, a cmdCancel_Click event that cancel all the changes into the textboxes (say empty the textboxes, reconnect them, and move the recordset on the last record), and so on.
Every step is to be carefully considered, and this is only my way to operate, but you can have a deeper control if you separate the various steps among the routines.
Hope it helps.
____________________________
Real Programmer can count up to 1024 on his fingers
|
|
16-06-2005 at 01:45 PM |
|
|
Ramya Level: Sage
 Registered: 08-04-2005 Posts: 60
|
Re: insert into MSAccess using recordset in VB
Sumathi
What s ur client system configuration.If its ask for updating just give as update and then continue with installing.
|
|
17-06-2005 at 05:25 AM |
|
|
lolly Level: Graduate
 Registered: 10-06-2005 Posts: 10
|
Re: insert into MSAccess using recordset in VB
I just thought i should point out that I particularly want to know how to inset and if statement into a recordset routine. Just so it is more specific...x
|
|
20-06-2005 at 09:15 AM |
|
|
lolly Level: Graduate
 Registered: 10-06-2005 Posts: 10
|
Re: insert into MSAccess using recordset in VB
You're a star! I kinda understand most of it..and ive been trying to manipulate it for my project..however in the form load event ive come across an error msg that says 'either BOF or EOF is true or the current record has been deleted. Requested operation requires a current record.'
the code is below:
Private Sub Form_Load()
' set the pointer during the load phase
Screen.MousePointer = vbHourglass
' create and open the connection
Set cn = New ADODB.Connection
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=" & App.Path & "\specifications.mdb;" _
& "Mode=ReadWrite|Share Deny None;Persist Security Info=False"
cn.Open
Set rs = New ADODB.Recordset
'The next step is to open the recordset
rs.CursorLocation = adUseClient
rs.Open "tblClients", cn, adOpenKeyset, adLockPessimistic, adCmdTable
'Cursor type is Keyset and is preferred in a multi-user database
'where changes by other users are not needed immediately
'The Lock Type is Pessimistic which ensures other users are locked
'out for the entire period that the records are being accessed and modified
rs.Sort = "Job#"
rs.MoveLast ' move to the last record
rsLastID = rs.Fields("Job#").Value ' ...and read its ID
' store in a variable the number of records
rsCount = rs.RecordCount
rs("Job#") = txtJobNo.Text
rs("Date") = txtDate.Text
rs("Sales Person") = dcbSalesPerson.Text
rs("Approx del date") = txtDeldate.Text
rs("Issue#") = txtIssueNo.Text
rs("Client") = txtClient.Text
rs("Client Address 1") = txtClientAddressLine1.Text
rs("Client Address 2") = txtClientAddressLine2.Text
rs("Client Address 3") = txtClientAddressLine3.Text
rs("Client Post Code") = txtClientPostCode.Text
rs("Telephone No") = txtClientTelNo.Text
rs("Contact") = txtClientContact.Text
rs("Site name") = txtSite.Text
rs("Site Address 1") = txtSiteAddressLine1.Text
rs("Site Address 2") = txtSiteAddressLine2.Text
rs("Site Address 3") = txtSiteAddressLine3.Text
rs("Site Telephone No") = txtSiteTelNo.Text
rs("Site Fax No") = txtSiteFaxNo.Text
rs("Site Contact") = txtSiteContact.Text
rs("Company representative") = txtSiteVisit.Text
rs("Other company contacts") = txtOtherCon.Text
rs("Existing Drawings") = optDrawYes.Value
rs("Existing Photos") = optPhotoYes.Value
rs("Foundation drawings") = txtFoundation.Text
rs("Layout drawings") = txtLayout.Text
rs("Cable drawings") = txtCable.Text
rs.MoveFirst 'this instruction has to be put to cause another event
' of the MoveComplete on the recordset in order to update the NumberOfRecord
' textbox
Screen.MousePointer = vbDefault
End Sub |
|
|
22-06-2005 at 04:09 PM |
|
|
Bharathi Level: Scholar
 Registered: 11-04-2005 Posts: 31
|
Re: insert into MSAccess using recordset in VB
Hi,
The steps are as follows.
1.Validate the data before inserting into tables.
2.Append a blank record in the table and assigns data entered in text boxes to fields of table and update the record to the table.
3.Clear the textbox controls.
If you want full code to develop a package , try this book
"Develop an accounting package using vb", try the client/server version. You will get to know how to create a single form and use it for many transactions, how to create a class for database application etc..
|
|
23-06-2005 at 06:11 AM |
|
|
lolly Level: Graduate
 Registered: 10-06-2005 Posts: 10
|
Re: insert into MSAccess using recordset in VB
Its the rs.MoveLast row.
No i dont know what im doing...lol..but i will try to just set the datafield property and datasource...but how will it know wot fields to insert my data into..if that sounds like a stupid question..pls forgive me but u hav to understand that i have no real IT skills apart from word processing and other such day to day skills....x
|
|
23-06-2005 at 07:44 AM |
|
|
Ramya Level: Sage
 Registered: 08-04-2005 Posts: 60
|
Re: insert into MSAccess using recordset in VB
Hai
CAn u please explian as what is ur exact expectation.
If u explain it in detail it will be easy for me to solve ur problem
|
|
23-06-2005 at 08:56 AM |
|
|
Goran Level: Moderator
 Registered: 16-05-2002 Posts: 1681
|
Re: insert into MSAccess using recordset in VB
quote:
rs.Sort = "Job#"
rs.MoveLast ' move to the last record |
Error is raised because sort method returned no rows, so you cant move to last record.
____________________________
If you find the answer helpful, please mark this topic as solved.
|
|
23-06-2005 at 11:58 PM |
|
|
Goran Level: Moderator
 Registered: 16-05-2002 Posts: 1681
|
Re: insert into MSAccess using recordset in VB
No need for adding record, you just check if it has an records returned first
if not (rs.bof and rs.eof) then
rs. move last
' and allthe rest of you code goes here
end if
____________________________
If you find the answer helpful, please mark this topic as solved.
|
|
24-06-2005 at 02:49 PM |
|
|
yronium Level: Moderator

 Registered: 14-04-2002 Posts: 907
|
Re: insert into MSAccess using recordset in VB
Yeah, of course every MoveXXXX method raises an error if the recordset is empty, so we got to manage this case.
Finally, my sample project is ready and everybody can download it here. In the zip file I provided two databases: an mdb with some records in, and another mdb that's empty. So you can test it in both cases. I'm exepecting readers report me any bug they find, but I made it as a tutorial on how managing an mdb database by VB and ADO.
It is a simple tutorial, so I removed all the Debug.Print instructions I used to test each step. If I wouldn't, it'd become more complex, but in fact I want to remind you that managing a db is not a basic topic, and if you want to get deep into it, you'd figure out each case and deal with all the various parameters, errors, events, status variables and other stuff ADO includes.
Hope it will be helpful and you like it.
____________________________
Real Programmer can count up to 1024 on his fingers
|
|
26-06-2005 at 02:47 PM |
|
|
|
|
 |
 |