Text boxs and tables

echo0001

Registered User.
Local time
Today, 07:36
Joined
Aug 30, 2008
Messages
55
Hello

I need the code for taking the value entred into a text box and then adding it to a new record in a table. this will happen on the OnClick event of a command button.

The text box is named "Text40"
The field the value will be added to is named "Allocated To"
The Table the field is located in is named "Allocated To"

Thank you for your time.
 
in on click event to your button add :

If Not IsNull(Me![Text40]) Then
Dim stSQL
stSQL = "insert into [Allocated To] ([Allocated To].[Allocated To])" &_
" Values (" & Me![Text40] & ")"
DoCmd.RunSQL stSQL
End If
 
Its coming back with complie error invalid character then its highlighting _

any help.
 
if on the OnClick event you are adding this value via another form try searching this forum for OpenArgs
 
Why not just bind the table to the form and set the control source of the text box to the field? Then, no code necessary, unless you want to validate.
 
now when i click the button it brings up an error and asks me to look at the DoCmd.RunSQL stSQL part of the code and debug it
 
Just a thought - it isn't good to have the table name the same as the field name (Allocated To). It might be wise to rename one of them slightly so that there is a difference.
 
table allocated to has been renamed to Allocated
 
yeah i know that i have changed it from "Allocated To" to "Allocated"
 
are there any references i have to add

No, but if your text box is text (not numeric or date then it would need to have quotes):

stSQL = "insert into [Allocated] ([Allocated].[Allocated To])" & _
" Values ('" & Me![Text40] & "')"
 
Last edited:
If Not IsNull(Me![Text40]) Then
Dim stSQL
stSQL = "insert into [Allocated To] ([Allocated To].[Allocated To])" & _
" Values ('" & Me![Text40] & "')"
DoCmd.RunSQL stSQL
End If

this one is sure to run
 
now i need the the opposite, to be able to remove a record using the same method.
any help?
 
If Not IsNull(Me![Text40]) Then
Dim stSQL
stSQL = "delete from [Allocated To] where [Allocated To].[Allocated To]=" & _
"'" & Me![Text40] & "'"
DoCmd.RunSQL stSQL
End If

I recommend that you read a little about sql if database programing is of an interest to you.
 
dont worry, i will. i have managed to get it to work using another method.
 

Users who are viewing this thread

Back
Top Bottom