Solved

VBA SQL doesn't recognise variable value

Posted on 2003-11-01
2
352 Views
Last Modified: 2012-08-14
I'm just learning VBA so I'm sure there is a simple answer to this.  I've got a form with abutton that fires a VBA procedure;


Option Compare Database

Private Sub yy_Click()
Dim strSQL As String
Static name As String
name = "ali"
strSQL = "INSERT INTO staff2 (NAME, PHONE) VALUES ( " & name & ", 02067737783)"
DoCmd.RunSQL (strSQL)

End Sub

When the button is clicked Access always pops a  "Enter parameter value" box with the word 'ali'  above an input field.  If I type a value into the input box the query is successfuly completed.

However, I really want the query to insert the value of the variable 'name', and not to ask me to input a value.  What am I doing wrong?

Cheers,
Ali
0
Comment
Question by:trident2
2 Comments
 
LVL 2

Expert Comment

by:proziath
ID: 9664336
Since name is a string it needs quotes around it, so you may need to do something like this

strSQL = "INSERT INTO staff2 (NAME, PHONE) VALUES ( " & "'"& name &"'" & ", 02067737783)"
0
 
LVL 1

Accepted Solution

by:
Mutare99 earned 125 total points
ID: 9699170
I imagine the NAME and PHONE fields are both text fields so this should do the trick:

strSQL = "INSERT INTO staff2 (NAME, PHONE) VALUES ('" & name & "','02067737783')"
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Can not delete, remove this shortcut, file 4 39
Bulk Reorder File Names 4 69
wireless name in LAN adapter 14 68
Need help with software deployments 3 62
I previously wrote an article addressing the use of UBCD4WIN and SARDU. All are great, but I have always been an advocate of SARDU. Recently it was suggested that I go back and take a look at Easy2Boot in comparison.
Workplace bullying has increased with the use of email and social media. Retain evidence of this with email archiving to protect your employees.
This video demonstrates basic masking and how to edit the mask to reveal the desired image.
This video demonstrates how to use each tool, their shortcuts, where and when to use them, and how to use the keyboard to improve workflow.

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question