add items

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lee123
    Contributor
    • Feb 2007
    • 556

    add items

    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
  • lee123
    Contributor
    • Feb 2007
    • 556

    #2
    by the way im useing visual basic 6.0 i would of used a datacontrol but it don't like access 2000

    Comment

    • vdraceil
      New Member
      • Jul 2007
      • 236

      #3
      Originally posted by lee123
      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.

      [CODE=vb]dim db as database
      dim rs as recordset

      Private Sub Form1_Load()
      set db=opendatabse( "\\yourfile.mdb ")
      set rs=db.openrecor dset("tablename ")
      ...
      End Sub

      Private Sub Command1_Click( )
      rs.addnew
      rs.fields(0)=te xt1.text
      rs.fields(1)=te xt2.text
      ...
      ...
      rs.update
      End Sub[/CODE]
      Try this!
      Experts will help you if you need to work with ADO.
      Last edited by Killer42; Dec 24 '07, 10:20 PM. Reason: Changed [CODE] to [CODE=vb]

      Comment

      • debasisdas
        Recognized Expert Expert
        • Dec 2006
        • 8119

        #4
        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.CONNECTIO N
        3. Open the connection
          con.Open "your connection string"
        4. con.Execute "insert into tablename(field 1,field2,....) values (value1,value2, .......)"
        Last edited by Killer42; Dec 24 '07, 10:23 PM. Reason: Inserted LIST=1 tag

        Comment

        • lee123
          Contributor
          • Feb 2007
          • 556

          #5
          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 (connectionstri ng) and click on the elipse and chose this:

          Provider=Micros oft.Jet.OLEDB.4 .0;Data Source=C:\Progr am Files\Microsoft Visual Studio\VB98\Tra ckYourBills.mdb ;Persist Security Info=False

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

          Originally posted by dedasisdas
          dim con as new ADODB.CONNECTIO N

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

          4.con.execute "insert into tablename(field 1,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).
          Last edited by Killer42; Dec 24 '07, 10:28 PM.

          Comment

          • lotus18
            Contributor
            • Nov 2007
            • 865

            #6
            Originally posted by lee123
            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:

            [CODE=vb]Public con As ADODB.Connectio n

            Public Sub SetConnection()
            Dim dbPath$ 'String
            dbPath = App.Path & "\Database\Your Database.mdb" 'Database path
            Set con = New ADODB.Connectio n 'Instantiate new object
            con.Open "Provider=Micro soft.Jet.OLEDB. 4.0;Data Source=" & dbPath & _
            ";Persist Security Info=False" 'Open the connection
            End Sub[/CODE]

            For adding new record
            [CODE=vb]
            'Assuming you are working with textboxes

            SetConnection 'Connection
            con.Execute "Insert Into TableName ([FieldName1],[FieldName2],[FieldNameN]) Values " _
            & "('" & Text1.Text & "','" & Text2.Text & "','" & TextN.Text & "')" [/CODE]
            OR
            [CODE=vb]SetConnection 'Connection
            Dim rs As New ADODB.Recordset
            rs.Open"Insert Into TableName ([FieldName1],[FieldName2],[FieldNameN]) Values " _
            & "('" & Text1.Text & "','" & Text2.Text & "','" & TextN.Text & "')", con, 3, 3
            [/CODE]

            Note: When you open any object you have to close it.
            Rey Sean
            Last edited by Killer42; Dec 24 '07, 10:31 PM.

            Comment

            • lee123
              Contributor
              • Feb 2007
              • 556

              #7
              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
              Last edited by Killer42; Dec 24 '07, 10:32 PM.

              Comment

              • lotus18
                Contributor
                • Nov 2007
                • 865

                #8
                Good luck. I'm sure you will get it right.


                Rey Sean

                Comment

                • debasisdas
                  Recognized Expert Expert
                  • Dec 2006
                  • 8119

                  #9
                  Hope lotus have solved your problem with the code.

                  Comment

                  • lee123
                    Contributor
                    • Feb 2007
                    • 556

                    #10
                    he did. thanks,

                    lee123

                    Comment

                    • lotus18
                      Contributor
                      • Nov 2007
                      • 865

                      #11
                      Originally posted by lee123
                      he did. thanks,

                      lee123
                      You're welcome :)


                      Rey Sean

                      Comment

                      Working...