Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 8th, 2003, 03:22 AM
Registered User
 
Join Date: Sep 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Linking form and a table that are build separately

Hi Fellows...

I have a form that I created separately with 2 textboxes namely
 Surname and FirstName.

I then created a table exactly with the same fields.

Now I want to add a Save Records button to the form and the data that I input on the form should be sent straight to the table. I DON'T WANT TO USE AN AUTO FORM...

I tried the

With/

!.textbox = Surname
.update
End With/

But then in the Dim statements there is a compile error for the rs statement.

Please help....



TK, Novice VBA Programmer, RSA
 
Old October 8th, 2003, 10:11 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Hi TK,

There are two ways to go: using SQL (preferred) or using DAO recordset object methods (probably slower in the long run).

Here's the SQL version. You just need a connection to execute the SQL on:

Private Sub cmdAdd_Click()

    Dim db As DAO.Database
    Dim strSQL As String

    If IsNull(Me!txtFirstName) Then
        MsgBox "First Name is a Required Entry.", 48
        Me!txtFirstName.SetFocus
        Exit Sub
    End If

    If IsNull(Me!txtSurName) Then
        MsgBox "Surname is a Required Entry.", 48
        Me!txtSurName.SetFocus
        Exit Sub
    End If

    Set db = CurrentDb

    strSQL = "INSERT INTO tblNames "
    strSQL = strSQL & "( FirstName, Surname )"
    strSQL = strSQL & " values ('"
    strSQL = strSQL & Me!txtFirstName & "','"
    strSQL = strSQL & Me!txtSurName & "'" & ");"

    ' MsgBox strSQL
    ' Debug.Print strSQL

    db.Execute strSQL

    MsgBox "Changes have been saved."

    Me!txtFirstName = ""
    Me!txtSurName = ""
    Me!txtFirstName.SetFocus

End Sub

Here's the DAO version. You need to instantiate a recordset object and call its methods (AddNew and Update):

Private Sub cmdAdd_Click()

    Dim db As DAO.Database
    Dim rstNames As DAO.Recordset
    Dim strSQL As String

    If IsNull(Me!txtFirstName) Then
        MsgBox "First Name is a Required Entry.", 48
        Me!txtFirstName.SetFocus
        Exit Sub
    End If

    If IsNull(Me!txtSurName) Then
        MsgBox "Surname is a Required Entry.", 48
        Me!txtSurName.SetFocus
        Exit Sub
    End If

    Set db = CurrentDb
    Set rstNames = db.OpenRecordset("tblNames", dbOpenDynaset)

    rstNames.AddNew
    rstNames("FirstName") = Me!txtFirstName
    rstNames("SurName") = Me!txtSurName
    rstNames.Update

    MsgBox "Changes have been saved."

    Me!txtFirstName = ""
    Me!txtSurName = ""
    Me!txtFirstName.SetFocus

End Sub

SQL will generally perform faster than equivalent operations using data access objects. Don't forget to set a reference to the DAO 3.x object library in your references dialog.

HTH,

Bob


 
Old October 8th, 2003, 10:18 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

You could also write your SQL string as:

strSQL = "INSERT INTO tblNames " & _
         "( FirstName, Surname )" & _
         " values ('" & _
         Me!txtFirstName & "','" & _
         Me!txtSurName & "'" & ");"

Faster yet.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Linking to a Foxpro Table DavidWE Access VBA 0 July 18th, 2008 02:26 PM
Linking 2 data fields from 1 table aRtware Access 7 May 28th, 2006 11:23 PM
linking form linh Access 3 February 9th, 2006 09:26 AM
Table designing and linking knight PHP Databases 0 January 17th, 2005 02:46 AM
Linking VB 6 form and Access table takawera VB Databases Basics 1 October 15th, 2004 09:12 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.