Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB Databases Basics
|
VB Databases Basics Beginning-level VB coding questions specific to using VB with databases. Issues not specific to database use will be redirected to other forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB Databases Basics 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 May 2nd, 2006, 12:04 PM
Registered User
 
Join Date: May 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Question about DoCmd.RUNSQL using an insert statem

Today is my first daying using access & VB6.0, and Im working with some visual basic 6.0 code.

Im trying to do a simple Insert for one of the tables that I have created, here is the code Im using:

Private Sub Command51_Click()
On Error GoTo Err_Command51_Click

DoCmd.RunSQL "Insert into test(nameF, nameL) values(me.firstname,me.lastname)"
    DoCmd.GoToRecord , , acNewRec

Exit_Command51_Click:
    Exit Sub

Err_Command51_Click:
    MsgBox Err.Description
    Resume Exit_Command51_Click

End Sub

For some reason, when I click on the button thats supposed to activate this piece of code, It displays an message box asking me to input the "me.firstname" and another box for the "me.lastname"..... Those 2 fields (first and last name) are enterable fields that I have created in one of my forms. Can someone help me out plz

thanks

 
Old May 5th, 2006, 10:15 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

You are sending the literal characters "m" "e" "." "f" "i" "r" (etc.) to the SQL parser.
You need to "unpack" the data:
Code:
Private Sub Command51_Click()

    On Error GoTo Er    ' Labels have procedure scope.  No need to use
                        ' so many letters.  (Personal preference...)
    DoCmd.RunSQL "Insert into test( nameF, nameL ) " & _
                 "values ( '" & me.firstname & "', '" & me.lastname & "')"

    DoCmd.GoToRecord , , acNewRec

Xt_Cmd51_Clk:
    Exit Sub

Er:
    MsgBox Err.Description
    Resume Xt_Cmd51_Clk

End Sub





Similar Threads
Thread Thread Starter Forum Replies Last Post
Using DoCmd.RunSQL to return values jscully Access VBA 10 August 21st, 2017 12:58 PM
Docmd.runsql anne.burrows VB How-To 2 October 25th, 2006 08:21 AM
DoCmd.RunSQL dates and time TarkaDahl Access VBA 3 May 11th, 2006 11:19 AM
docmd.runsql "select RodMead Access VBA 2 July 31st, 2004 02:55 PM
Help with DoCmd.RunSQL command ricmar Access VBA 3 July 21st, 2004 03:32 PM





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