dbTalk Databases Forums  

INSERT INTO statement

comp.databases.ms-access comp.databases.ms-access


Discuss INSERT INTO statement in the comp.databases.ms-access forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
FireyColin
 
Posts: n/a

Default INSERT INTO statement - 11-25-2011 , 06:07 AM






Can anyone help with the following statement? I am clearly declaring
the strings incorrectly but can't work out how.


Dim StrValue1 As String
Dim StrValue2 As String

StrValue1 = "[Forms]![frmPolicy]![txtPolicyNo]"
StrValue2 = "[Forms]![frmPolicy]![txtPolicyVer]"

strSQL = "INSERT INTO ProfitShareReInsured (policy_no, version_no,
organisation_id) VALUES ('StrValue1', 'StrValue2', '809')"

DoCmd.RunSQL strSQL

Reply With Quote
  #2  
Old   
Bob Barrows
 
Posts: n/a

Default Re: INSERT INTO statement - 11-25-2011 , 07:13 AM






FireyColin wrote:
Quote:
Can anyone help with the following statement? I am clearly declaring
the strings incorrectly but can't work out how.


Dim StrValue1 As String
Dim StrValue2 As String

StrValue1 = "[Forms]![frmPolicy]![txtPolicyNo]"
StrValue2 = "[Forms]![frmPolicy]![txtPolicyVer]"
Assuming you wish the values contained in those form controls to be assigned
to the variables, you have to remove the delimiters (the quotes).

StrValue1 = [Forms]![frmPolicy]![txtPolicyNo]
StrValue2 = [Forms]![frmPolicy]![txtPolicyVer]

'check the values of the variables - comment when
'finished debugging
msgbox strvalue1
msgbox strvalue2

Quote:
strSQL = "INSERT INTO ProfitShareReInsured (policy_no, version_no,
organisation_id) VALUES ('StrValue1', 'StrValue2', '809')"
In this case the problem is different: your statement is inserting the
literal strings 'StrValue1' and 'StrValue2'. You need to concatenate the
values into the sql string:

strSQL = "INSERT INTO ProfitShareReInsured " & _
"(policy_no, version_no, organisation_id) VALUES ('" & _
StrValue1 & "', '" & StrValue2 & "', '809')"

'again - check the value assigned to strSQL
'comment when finished debugging:

msgbox strSQL

Incidently, if the user enters a value containing an apostrophe into either
form control, this will fail. To foolproof it, you need to escape any
apostrophes in the strings by doubling them. You can use the replace()
function to do it, like this:

StrValue1 = [Forms]![frmPolicy]![txtPolicyNo]
StrValue1 = replace(StrValue1, "'","''")
StrValue2 = [Forms]![frmPolicy]![txtPolicyVer]
StrValue1 = replace(StrValue2, "'","''")

Reply With Quote
  #3  
Old   
FireyColin
 
Posts: n/a

Default Re: INSERT INTO statement - 11-25-2011 , 09:40 AM



On Nov 25, 1:13*pm, "Bob Barrows" <reb01... (AT) NOSPAMyahoo (DOT) com> wrote:
Quote:
FireyColin wrote:
Can anyone help with the following statement? *I am clearly declaring
the strings incorrectly but can't work out how.

Dim StrValue1 As String
Dim StrValue2 As String

StrValue1 = "[Forms]![frmPolicy]![txtPolicyNo]"
StrValue2 = "[Forms]![frmPolicy]![txtPolicyVer]"

Assuming you wish the values contained in those form controls to be assigned
to the variables, you have to remove the delimiters (the quotes).

StrValue1 = [Forms]![frmPolicy]![txtPolicyNo]
StrValue2 = [Forms]![frmPolicy]![txtPolicyVer]

'check the values of the variables - comment when
'finished debugging
msgbox strvalue1
msgbox strvalue2



strSQL = "INSERT INTO ProfitShareReInsured (policy_no, version_no,
organisation_id) VALUES ('StrValue1', 'StrValue2', '809')"

In this case the problem is different: your statement is inserting the
literal strings 'StrValue1' and 'StrValue2'. You need to concatenate the
values into the sql string:

strSQL = "INSERT INTO ProfitShareReInsured " & _
"(policy_no, version_no, organisation_id) VALUES ('" & _
StrValue1 & "', '" & StrValue2 & "', '809')"

'again - check the value assigned to strSQL
'comment when finished debugging:

msgbox strSQL

Incidently, if the user enters a value containing an apostrophe into either
form control, this will fail. To foolproof it, you need to escape any
apostrophes in the strings by doubling them. You can use the replace()
function to do it, like this:

StrValue1 = [Forms]![frmPolicy]![txtPolicyNo]
StrValue1 = replace(StrValue1, "'","''")
StrValue2 = [Forms]![frmPolicy]![txtPolicyVer]
StrValue1 = replace(StrValue2, "'","''")
Many Thanks Bob, I knew it would be something simple.

Reply With Quote
Reply




Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.