Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 8th, 2003, 03:22 AM
Registered User
 
Join Date: Sep 2003
Location: Johannesburg, Gauteng, South Africa.
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
Reply With Quote
  #2 (permalink)  
Old October 8th, 2003, 10:11 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
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


Reply With Quote
  #3 (permalink)  
Old October 8th, 2003, 10:18 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
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.

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 08:53 AM.


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