DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2
  1. #1
    Join Date
    May 2005

    Question data access on Visual Basic 6

    Gentlemen, I知 having a problem I hope you can help me with. I知 writing a program that reads data from a text file and puts it into a Access database via the data control. I have a data1 control on the form.
    I have the program to the point where it reads the data from the text file. At that point it displays it on textbox controls...text1..text2..etc so the user can make any changes.
    My question is how can I get strings into the datafile?

    I have looked at several examples and all the samples I found start with databound textboxes that display the data into textboxes until it reaches the 船ata1.Recordset.AddNew then it blanks it out them out. I知 an amateur at this so I知 sure there must be a good way to do it.

    Also the database that it updates MUST overwrite any entry with the same name if possible, not create a duplicate.

    If you guys are not following this I can upload a sample.


  2. #2
    Join Date
    Jun 2005
    New Zealand

    Re: Data Access in Visual Basic 6.0


    First off I want to re-cap on some of the things you mentioned in your first post to ensure that we are both of mutual understanding of your problem.

    I understand that you are taking information from a text file, allowing it to be modified by the user and then when specified by the user, the information is then passed to a Database and stored.

    If that covers it, the solution for this is a fairly simple method.

    Okay, the method I am going to describe is a common DAO code concept, in a sequence, you create your project, design a data structure for your Microsoft Access Database. From there you want to add a Reference to the DAO Library in your project and then wrap procedure/function code around an event (such as cmdSave_Click).

    Now, start up a new project and go up to 'Project' in the menu bar, go down the list and select 'References'. Scroll down the list of references and when you find it, tick the one called 'Microsoft DAO 2.5/3.51 Compatibility Library', once you have found and ticked this, click 'OK'. Any other references that are already ticked or unticked can be left as they are.

    You have:

    - Created a Standard EXE Project
    - You have named your Project
    - A reference has been made to the Microsoft DAO Library

    Moving on, now that you have done all this, get a few of the cosmetic issues out of the way. First of all, take the default form that is created and rename it as 'frmSaveRecord', it is always good to give forms meaningful and descriptive names. Next you want to continue that habbit by giving your form a descriptive Caption Text, such as 'Save Data to Database Record'.

    Now you need to add the specific objects (such as Text Boxes and Command Buttons) to represent and format data from the text files. For the purpose of this demonstration, I will assume that you have 5 pieces of data that are grouped into a single category.

    - First Name
    - Last Name
    - Telephone
    - Mobile
    - Address

    Usually it is best to have arranged your Data structure and created the Database first, but because this is such a simple Project we can do the reverse to make things more easily understood.

    Place 5 text boxes onto your first along with 5 labels, give each text box and label a proper name, labels aren't so important (naming wise) but it is good to be tidy when it comes to writing enteprise applications -- my speciality.




    The captions for your labels should be obvious, "First Name" and "Last Name", you also want to apply a colon to keep things tidy 'First Name: '.

    Arrange these objects as you see fit, obviously you want the label to visually correspond with the appropriate text box. After you have done that, go ahead and add 2 command buttons, name one cmdSaveRecord and the other cmdCancelWindow, the caption for each should be 'Save' and 'Cancel', although you give them a descriptive name, the user doesn't need an as description caption.

    The simple reason for giving objects names like that (prefixed with their standard naming conventions) is to create easier to read code, code that is easy to read is easy to modify and link with.

    Now, go ahead and create your Data Structure in the Database. I suggest creating a single Table and adding the 5 fields and giving them the standard Text property with a size of 50 characters. Name the table "Contact", I will use that as an example further on.

    You have:

    - Designed your User Interface
    - Started what will be emmaculate Code (descriptive object naming)
    - Created a Data Structure and your Database

    The final part, for this I expect you to have reasonable knowledge of the VB Language, though not advanced, this code is relatively simple.

    This is what the code in your cmdSaveRecord Procedure will look like.

    Private Sub cmdSaveRecord_Click()
    Dim dbContact As Database
    Dim recContact As Recordset

    Set dbContact = OpenDatabase("C:\Database\Data.mdb")
    Set recContact = dbContact.OpenRecordset("Contact", dbOpenDynaset)

    recContact("First Name") = txtFirstName.Text
    recContact("Last Name") = txtLastName.Text
    recContact("Telephone") = txtTelephone.Text
    recContact("Mobile") = txtMobile.Text
    recContact("Address") = txtAddress.Text

    End Sub

    Whatever you put in the cmdCancelWindow procedure is entirely up to you, something simple like 'Unload frmSaveRecord'.

    To break down the above code I am going to step through the most important lines and then cap off with a full description.

    First of all, you declare two objects, one as a Database and the other as a Recordset, the next two (set) lines below this configure the newly created objects. As you can see, the first line opens the database, you simply specify the path of it in between the " " quote tags. Below this, you are opening the recordset, the only part of this (there is only one part like the database line) that concerns you is where you specify what table you want to access. In this case, it is your 'Contact' table that is located in your database.

    Below this is the AddNew tag, there are several to select from, such as .edit and .delete, with .edit and .addnew you must finish off with .update, or else your information is not saved in the database properly.

    Inbetween, you will see the references to each field within the database table, there are two methods for passing information to the field and the above is one of them (and my favorite). The other choise is:

    recContact!FirstName = txtFirstName.Text

    The only reason I used the above is so that you can specify field names with spaces in them. To the best of my knowledge, you cannot use field names with spaces when the above (recContact!FirstName) is used.

    That is pretty much it.

    Retreiving data from the database takes on a similar approach with several differences. If you would like me to, I can give you a complete tutorial of working with the DAO Library in code.

    I hope this helps!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center