Populate a form using a recordset

K

krisjuchau

I am trying to populate a form using a recordset but keep on getting
the same error:

Run time error 3061. Too few parameters.

Here is the code:

Private Sub Form_Open(Cancel As Integer)

Dim strDevID As String
Dim strDevIDValue As String

Dim db As Database
Dim rs As Recordset
Dim tmpUser As String

If Not IsNull(Me.OpenArgs) Then

strDevID = Me.OpenArgs

Me.txtDevID.Value = strDevID

End If



Set db = CurrentDb
Set rs = db.OpenRecordset("SELECT tblDevelop.Cust_ID,
tblDevelop.CustName FROM tblDevelop WHERE tblDevelop.DevID =
Me.txtDevID.Value")

Me.txtCustID.Value = rs!Cust_ID
Me.txtCust.Value = rs!CustName

rs.Close

Set rs = Nothing
Set db = Nothing



End Sub

Private Sub txtCust_BeforeUpdate(Cancel As Integer)

End Sub
 
D

Dirk Goldgar

I am trying to populate a form using a recordset but keep on getting
the same error:

Run time error 3061. Too few parameters.

Here is the code:

Private Sub Form_Open(Cancel As Integer)

Dim strDevID As String
Dim strDevIDValue As String

Dim db As Database
Dim rs As Recordset
Dim tmpUser As String

If Not IsNull(Me.OpenArgs) Then

strDevID = Me.OpenArgs

Me.txtDevID.Value = strDevID

End If



Set db = CurrentDb
Set rs = db.OpenRecordset("SELECT tblDevelop.Cust_ID,
tblDevelop.CustName FROM tblDevelop WHERE tblDevelop.DevID =
Me.txtDevID.Value")

Me.txtCustID.Value = rs!Cust_ID
Me.txtCust.Value = rs!CustName

rs.Close

Set rs = Nothing
Set db = Nothing



End Sub

Private Sub txtCust_BeforeUpdate(Cancel As Integer)

End Sub


You need to move the value of Me.txtDevID outside of the quotation marks of
the literal SQL string, so that the value, not the name, is concatenated
into the string. Instead of this:
Set rs = db.OpenRecordset("SELECT tblDevelop.Cust_ID,
tblDevelop.CustName FROM tblDevelop WHERE tblDevelop.DevID =
Me.txtDevID.Value")

.... try this:

Set rs = db.OpenRecordset( _
"SELECT Cust_ID, CustName FROM tblDevelop " & _
"WHERE DevID = " & Me.txtDevID.Value)

That's assuming that DevID is a numeric field. If it's a text field, you
need to embed quotes around the value:

Set rs = db.OpenRecordset( _
"SELECT Cust_ID, CustName FROM tblDevelop " & _
"WHERE DevID = '" & Me.txtDevID.Value & "'")

In the above, I'm using single-quotes for the quote character. If the value
might include the single-quote character, you can use double-quotes instead,
though it's trickier to get a double-quote inside a quoted string literal.
I'd do it by using the Chr() function to specify the character, like this:

Set rs = db.OpenRecordset( _
"SELECT Cust_ID, CustName FROM tblDevelop " & _
"WHERE DevID = " & Chr(34) & Me.txtDevID.Value & Chr(34))
 
K

krisjuchau

You need to move the value of Me.txtDevID outside of the quotation marks of
the literal SQL string, so that the value, not the name, is concatenated
into the string.  Instead of this:


... try this:

    Set rs = db.OpenRecordset( _
        "SELECT Cust_ID, CustName FROM tblDevelop " & _
        "WHERE DevID = " & Me.txtDevID.Value)

That's assuming that DevID is a numeric field.  If it's a text field, you
need to embed quotes around the value:

    Set rs = db.OpenRecordset( _
        "SELECT Cust_ID, CustName FROM tblDevelop " & _
        "WHERE DevID = '" & Me.txtDevID.Value & "'")

In the above, I'm using single-quotes for the quote character.  If the value
might include the single-quote character, you can use double-quotes instead,
though it's trickier to get a double-quote inside a quoted string literal..
I'd do it by using the Chr() function to specify the character, like this:

    Set rs = db.OpenRecordset( _
        "SELECT Cust_ID, CustName FROM tblDevelop " & _
        "WHERE DevID = " & Chr(34) & Me.txtDevID.Value & Chr(34))

Thanks that worked. Now I am trying to modify my initial instert
statement in order to update the table, I am missing something but not
quite sure what, here is the code:

Private Sub cmdSubmit_Click()

Dim strCustID, strCustName, strQ1, strQ2, strQ3, strQ4, strQ5 As
String
Dim strQ6, strQ7, strQ8, strQ9, strQ10, strQ11, strQ12 As String
Dim strQ13, strQ14, strType As String
Dim strInsertSQL, strCustComp As String
Dim intDate As Date

strCustID = Me.txtCustID.Value & ""
strCustName = Me.txtName.Value & ""
strQ1 = Me.txtQ1.Value & ""
strQ2 = Me.txtQ2.Value & ""
strQ3 = Me.txtQ3.Value & ""
strQ4 = Me.txtQ4.Value & ""
strQ5 = Me.txtQ5.Value & ""
strQ6 = Me.txtQ6.Value & ""
strQ7 = Me.txtQ7.Value & ""
strQ8 = Me.txtQ8.Value & ""
strQ9 = Me.txtQ9.Value & ""
strQ10 = Me.txtQ10.Value & ""
strQ11 = Me.txtQ11.Value & ""
strQ12 = Me.txtQ12.Value & ""
strQ13 = Me.txtQ13.Value & ""
strQ14 = Me.txtQ14.Value & ""
strType = Me.txtType.Value & ""
strDate = Me.txtDate.Value & ""
strCustComp = Me.txtCust.Value & ""

strInsertSQL = "UPDATE tblDevelop [CustName],[Q1S],[Q2S]," & _
"[Q3S],[Q4S],[Q5S],[Q6S],[Q7S],[Q8S], [Q9S], [Q10S],
[Q11S], [Q12S], [Q13S], [Q14S]," & _
"[TYPE], [DATE], [CustComp]) " & _
"VALUES (" & Chr$(34) & strCustName & Chr$(34) & ",
" & Chr$(34) & strQ1 & Chr$(34) & ", " & Chr$(34) & strQ2 & Chr$(34) &
", " & _
"'" & strQ3 & "', '" & strQ4 & "', '" & strQ5 & "',
'" & strQ6 & "', '" & strQ7 & "', " & _
"'" & strQ8 & "', '" & strQ9 & "', '" & strQ10 & "',
'" & strQ11 & "', '" & strQ12 & "', '" & strQ13 & "', '" & strQ14 &
"', '" & strType & "', '" & strDate & " ', " & Chr$(34) & strCustComp
& Chr$(34) & ") WHERE tblDevelop.DevID = " & Me.txtDevID.Value





Call DoCmd.RunSQL(strInsertSQL)

MsgBox "Thank You"

End Sub
 
D

Dirk Goldgar

It would have been better to post this entirely new question as a new
thread.

At the very least, you are missing an opening parenthesis before the field
list said:
strInsertSQL = "UPDATE tblDevelop [CustName],[Q1S],[Q2S]," & _

That should have been:

strInsertSQL = "UPDATE tblDevelop ([CustName],[Q1S],[Q2S]," & _

I see that you have a closing parenthesis, so I think this is just a typo.

I see a couple of other things that may not be hurting you directly, but
that are worth mentioning.

1. In these statements:
Dim strCustID, strCustName, strQ1, strQ2, strQ3, strQ4, strQ5 As String
Dim strQ6, strQ7, strQ8, strQ9, strQ10, strQ11, strQ12 As String
Dim strQ13, strQ14, strType As String
Dim strInsertSQL, strCustComp As String

.... only strQ5, strQ12, strType, and strCustComp are being declared as
String. The rest are all being declared as Variant, which is probably not
what you want. In VB, declarations don't factor that way; you have to
supply the type for every name declared, or have it default to Variant.

2. In this statement:
Dim intDate As Date

.... why are you using the "int" prefix, which usually connoted an Integer
variable, rather than a Date? Most people who use this naming convention
would use the "dt" prefix: dtDate. On the other hand, I notice that your
code later on is working with a variable called "strDate", which is not
declared. Was that intended to be this same variable? If so, and if it's
holding a date/time value, why are you treating it as a string?

3. Any time I see numbered fields, as in Q1, Q2, Q3, etc., I conclude that
the table design is not normalized. Depending on what you want to do with
these data and how you anticipate querying them, you probably should
consider normalizing your table design so that each question (if that's what
"Q" stands for) is stored in a separate record in a related table. That, of
course, is a larger change and would have an impact on your design in many
places.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)



Thanks that worked. Now I am trying to modify my initial instert
statement in order to update the table, I am missing something but not
quite sure what, here is the code:

Private Sub cmdSubmit_Click()

Dim strCustID, strCustName, strQ1, strQ2, strQ3, strQ4, strQ5 As
String
Dim strQ6, strQ7, strQ8, strQ9, strQ10, strQ11, strQ12 As String
Dim strQ13, strQ14, strType As String
Dim strInsertSQL, strCustComp As String
Dim intDate As Date

strCustID = Me.txtCustID.Value & ""
strCustName = Me.txtName.Value & ""
strQ1 = Me.txtQ1.Value & ""
strQ2 = Me.txtQ2.Value & ""
strQ3 = Me.txtQ3.Value & ""
strQ4 = Me.txtQ4.Value & ""
strQ5 = Me.txtQ5.Value & ""
strQ6 = Me.txtQ6.Value & ""
strQ7 = Me.txtQ7.Value & ""
strQ8 = Me.txtQ8.Value & ""
strQ9 = Me.txtQ9.Value & ""
strQ10 = Me.txtQ10.Value & ""
strQ11 = Me.txtQ11.Value & ""
strQ12 = Me.txtQ12.Value & ""
strQ13 = Me.txtQ13.Value & ""
strQ14 = Me.txtQ14.Value & ""
strType = Me.txtType.Value & ""
strDate = Me.txtDate.Value & ""
strCustComp = Me.txtCust.Value & ""

strInsertSQL = "UPDATE tblDevelop [CustName],[Q1S],[Q2S]," & _
"[Q3S],[Q4S],[Q5S],[Q6S],[Q7S],[Q8S], [Q9S], [Q10S],
[Q11S], [Q12S], [Q13S], [Q14S]," & _
"[TYPE], [DATE], [CustComp]) " & _
"VALUES (" & Chr$(34) & strCustName & Chr$(34) & ",
" & Chr$(34) & strQ1 & Chr$(34) & ", " & Chr$(34) & strQ2 & Chr$(34) &
", " & _
"'" & strQ3 & "', '" & strQ4 & "', '" & strQ5 & "',
'" & strQ6 & "', '" & strQ7 & "', " & _
"'" & strQ8 & "', '" & strQ9 & "', '" & strQ10 & "',
'" & strQ11 & "', '" & strQ12 & "', '" & strQ13 & "', '" & strQ14 &
"', '" & strType & "', '" & strDate & " ', " & Chr$(34) & strCustComp
& Chr$(34) & ") WHERE tblDevelop.DevID = " & Me.txtDevID.Value





Call DoCmd.RunSQL(strInsertSQL)

MsgBox "Thank You"

End Sub
 
D

Dirk Goldgar

I just looked at this again and realized that you're using an UPDATE
statement, not an INSERT statment, so the syntax is wholly wrong. Sorry
about that. You'd need something like

strInsertSQL = "UPDATE tblDevelop SET " & _
"[CustName]=" & Chr$(34) & strCustName & Chr$(34) & _
", [Q1S]=" & Chr$(34) & strQ1 & Chr$(34) & _
", [Q2S]=" & Chr$(34) & strQ2 & Chr$(34) & _
", [Q3S]='" & strQ3 & "'" & _
", [Q4S]='" & strQ4 & "'" & _
", [Q5S]='" & strQ5 & "'" & _
", [Q6S]='" & strQ6 & "'" & _
", [Q7S]='" & strQ7 & "'" & _
", [Q8S]='" & strQ8 & "'" & _
", [Q9S]='" & strQ9 & "'" & _
", [Q10S]='" & strQ10 & "'" & _
", [Q11S]='" & strQ11 & "'" & _
", [Q12S]='" & strQ12 & "'" & _
", [Q13S]='" & strQ13 & "'" & _
", [Q14S]='" & strQ14 & "'" & _
", [TYPE]='" & strType & _
", [DATE]=#" & Format(dtDate, "MM/DD/YYYY") & "#" & _
", [CustComp]=" & Chr(34) & strCustComp & Chr(34) & _
" WHERE tblDevelop.DevID = " & Me.txtDevID.Value


--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)


Dirk Goldgar said:
It would have been better to post this entirely new question as a new
thread.

At the very least, you are missing an opening parenthesis before the field
list said:
strInsertSQL = "UPDATE tblDevelop [CustName],[Q1S],[Q2S]," & _

That should have been:

strInsertSQL = "UPDATE tblDevelop ([CustName],[Q1S],[Q2S]," & _

I see that you have a closing parenthesis, so I think this is just a typo.

I see a couple of other things that may not be hurting you directly, but
that are worth mentioning.

1. In these statements:
Dim strCustID, strCustName, strQ1, strQ2, strQ3, strQ4, strQ5 As String
Dim strQ6, strQ7, strQ8, strQ9, strQ10, strQ11, strQ12 As String
Dim strQ13, strQ14, strType As String
Dim strInsertSQL, strCustComp As String

... only strQ5, strQ12, strType, and strCustComp are being declared as
String. The rest are all being declared as Variant, which is probably not
what you want. In VB, declarations don't factor that way; you have to
supply the type for every name declared, or have it default to Variant.

2. In this statement:
Dim intDate As Date

... why are you using the "int" prefix, which usually connoted an Integer
variable, rather than a Date? Most people who use this naming convention
would use the "dt" prefix: dtDate. On the other hand, I notice that your
code later on is working with a variable called "strDate", which is not
declared. Was that intended to be this same variable? If so, and if it's
holding a date/time value, why are you treating it as a string?

3. Any time I see numbered fields, as in Q1, Q2, Q3, etc., I conclude that
the table design is not normalized. Depending on what you want to do with
these data and how you anticipate querying them, you probably should
consider normalizing your table design so that each question (if that's
what "Q" stands for) is stored in a separate record in a related table.
That, of course, is a larger change and would have an impact on your
design in many places.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)



Thanks that worked. Now I am trying to modify my initial instert
statement in order to update the table, I am missing something but not
quite sure what, here is the code:

Private Sub cmdSubmit_Click()

Dim strCustID, strCustName, strQ1, strQ2, strQ3, strQ4, strQ5 As
String
Dim strQ6, strQ7, strQ8, strQ9, strQ10, strQ11, strQ12 As String
Dim strQ13, strQ14, strType As String
Dim strInsertSQL, strCustComp As String
Dim intDate As Date

strCustID = Me.txtCustID.Value & ""
strCustName = Me.txtName.Value & ""
strQ1 = Me.txtQ1.Value & ""
strQ2 = Me.txtQ2.Value & ""
strQ3 = Me.txtQ3.Value & ""
strQ4 = Me.txtQ4.Value & ""
strQ5 = Me.txtQ5.Value & ""
strQ6 = Me.txtQ6.Value & ""
strQ7 = Me.txtQ7.Value & ""
strQ8 = Me.txtQ8.Value & ""
strQ9 = Me.txtQ9.Value & ""
strQ10 = Me.txtQ10.Value & ""
strQ11 = Me.txtQ11.Value & ""
strQ12 = Me.txtQ12.Value & ""
strQ13 = Me.txtQ13.Value & ""
strQ14 = Me.txtQ14.Value & ""
strType = Me.txtType.Value & ""
strDate = Me.txtDate.Value & ""
strCustComp = Me.txtCust.Value & ""

strInsertSQL = "UPDATE tblDevelop [CustName],[Q1S],[Q2S]," & _
"[Q3S],[Q4S],[Q5S],[Q6S],[Q7S],[Q8S], [Q9S], [Q10S],
[Q11S], [Q12S], [Q13S], [Q14S]," & _
"[TYPE], [DATE], [CustComp]) " & _
"VALUES (" & Chr$(34) & strCustName & Chr$(34) & ",
" & Chr$(34) & strQ1 & Chr$(34) & ", " & Chr$(34) & strQ2 & Chr$(34) &
", " & _
"'" & strQ3 & "', '" & strQ4 & "', '" & strQ5 & "',
'" & strQ6 & "', '" & strQ7 & "', " & _
"'" & strQ8 & "', '" & strQ9 & "', '" & strQ10 & "',
'" & strQ11 & "', '" & strQ12 & "', '" & strQ13 & "', '" & strQ14 &
"', '" & strType & "', '" & strDate & " ', " & Chr$(34) & strCustComp
& Chr$(34) & ") WHERE tblDevelop.DevID = " & Me.txtDevID.Value





Call DoCmd.RunSQL(strInsertSQL)

MsgBox "Thank You"

End Sub
 
K

krisjuchau

I just looked at this again and realized that you're using an UPDATE
statement, not an INSERT statment, so the syntax is wholly wrong.  Sorry
about that.  You'd need something like

    strInsertSQL = "UPDATE tblDevelop SET " & _
        "[CustName]=" & Chr$(34) & strCustName & Chr$(34) & _
        ", [Q1S]=" & Chr$(34) & strQ1 & Chr$(34)  & _
        ", [Q2S]=" & Chr$(34) & strQ2 & Chr$(34)  & _
        ", [Q3S]='" & strQ3 & "'"  & _
        ", [Q4S]='" & strQ4 & "'"  & _
        ", [Q5S]='" & strQ5 & "'"  & _
        ", [Q6S]='" & strQ6 & "'"  & _
        ", [Q7S]='" & strQ7 & "'"  & _
        ", [Q8S]='" & strQ8 & "'"  & _
        ", [Q9S]='" & strQ9 & "'"  & _
        ", [Q10S]='" & strQ10 & "'"  & _
        ", [Q11S]='" & strQ11 & "'"  & _
        ", [Q12S]='" & strQ12 & "'"  & _
        ", [Q13S]='" & strQ13 & "'"  & _
        ", [Q14S]='" & strQ14 & "'"  & _
        ", [TYPE]='" & strType & _
        ", [DATE]=#" & Format(dtDate, "MM/DD/YYYY") & "#" & _
        ", [CustComp]=" & Chr(34) &  strCustComp & Chr(34) & _
    " WHERE tblDevelop.DevID = " & Me.txtDevID.Value

--
Dirk Goldgar, MS Access MVPwww.datagnostics.com

(please reply to the newsgroup)


It would have been better to post this entirely new question as a new
thread.
At the very least, you are missing an opening parenthesis before the field
list in your SQL statement.  You wrote:
strInsertSQL = "UPDATE tblDevelop [CustName],[Q1S],[Q2S]," & _
That should have been:
   strInsertSQL = "UPDATE tblDevelop ([CustName],[Q1S],[Q2S]," & _
I see that you have a closing parenthesis, so I think this is just a typo.
I see a couple of other things that may not be hurting you directly, but
that are worth mentioning.
1. In these statements:
... only strQ5, strQ12, strType, and strCustComp are being declared as
String.  The rest are all being declared as Variant, which is probably not
what you want.  In VB, declarations don't factor that way;  you have to
supply the type for every name declared, or have it default to Variant.
2.  In this statement:
... why are you using the "int" prefix, which usually connoted an Integer
variable, rather than a Date?  Most people who use this naming convention
would use the "dt" prefix:  dtDate.  On the other hand, I notice that your
code later on is working with a variable called "strDate", which is not
declared.  Was that intended to be this same variable?  If so, and if it's
holding a date/time value, why are you treating it as a string?
3. Any time I see numbered fields, as in Q1, Q2, Q3, etc., I conclude that
the table design is not normalized.  Depending on what you want to dowith
these data and how you anticipate querying them, you probably should
consider normalizing your table design so that each question (if that's
what "Q" stands for) is stored in a separate record in a related table.
That, of course, is a larger change and would have an impact on your
design in many places.
(please reply to the newsgroup)
news:262ed088-9dd3-4cf7-b6e9-dd6b2aba6d62@r37g2000prr.googlegroups.com....
Thanks that worked. Now I am trying to modify my initial instert
statement in order to update the table, I am missing something but not
quite sure what, here is the code:
Private Sub cmdSubmit_Click()
Dim strCustID, strCustName, strQ1, strQ2, strQ3, strQ4, strQ5 As
String
Dim strQ6, strQ7, strQ8, strQ9, strQ10, strQ11, strQ12 As String
Dim strQ13, strQ14, strType As String
Dim strInsertSQL, strCustComp As String
Dim intDate As Date
strCustID = Me.txtCustID.Value & ""
strCustName = Me.txtName.Value & ""
strQ1 = Me.txtQ1.Value & ""
strQ2 = Me.txtQ2.Value & ""
strQ3 = Me.txtQ3.Value & ""
strQ4 = Me.txtQ4.Value & ""
strQ5 = Me.txtQ5.Value & ""
strQ6 = Me.txtQ6.Value & ""
strQ7 = Me.txtQ7.Value & ""
strQ8 = Me.txtQ8.Value & ""
strQ9 = Me.txtQ9.Value & ""
strQ10 = Me.txtQ10.Value & ""
strQ11 = Me.txtQ11.Value & ""
strQ12 = Me.txtQ12.Value & ""
strQ13 = Me.txtQ13.Value & ""
strQ14 = Me.txtQ14.Value & ""
strType = Me.txtType.Value & ""
strDate = Me.txtDate.Value & ""
strCustComp = Me.txtCust.Value & ""
strInsertSQL = "UPDATE tblDevelop [CustName],[Q1S],[Q2S]," & _
                 "[Q3S],[Q4S],[Q5S],[Q6S],[Q7S],[Q8S], [Q9S], [Q10S],
[Q11S], [Q12S], [Q13S], [Q14S]," & _
                 "[TYPE], [DATE], [CustComp]) " & _
                 "VALUES (" & Chr$(34) & strCustName & Chr$(34) & ",
" & Chr$(34) & strQ1 & Chr$(34) & ", " & Chr$(34) & strQ2 & Chr$(34) &
", " & _
                 "'" & strQ3 & "', '" & strQ4 & "', '" & strQ5 & "',
'" & strQ6 & "', '" & strQ7 & "', " & _
                 "'" & strQ8 & "', '" & strQ9 & "', '" & strQ10 & "',
'" & strQ11 & "', '" & strQ12 & "', '" & strQ13 & "', '" & strQ14 &
"', '" & strType & "', '" & strDate & " ', " & Chr$(34) & strCustComp
& Chr$(34) & ") WHERE tblDevelop.DevID = " & Me.txtDevID.Value
   Call DoCmd.RunSQL(strInsertSQL)
MsgBox "Thank You"

I went a little different route based on some other advice. Thanks for
your help and here is the final code:

Private Sub cmdSubmit_Click()

Dim strUpdateSQL As String

With Me
strUpdateSQL = _
" UPDATE tblDevelop SET " & _
" [CustName] = " & """" & .txtName & """," & _
" [Q1S] = " & """" & .txtQ1 & """," & _
" [Q2S] = " & """" & .txtQ2 & """," & _
" [Q3S] = " & """" & .txtQ3 & """," & _
" [Q4S] = " & """" & .txtQ4 & """," & _
" [Q5S] = " & """" & .txtQ5 & """," & _
" [Q6S] = " & """" & .txtQ6 & """," & _
" [Q7S] = " & """" & .txtQ7 & """," & _
" [Q8S] = " & """" & .txtQ8 & """," & _
" [Q9S] = " & """" & .txtQ9 & """," & _
" [Q10S] = " & """" & .txtQ10 & """," & _
" [Q11S] = " & """" & .txtQ11 & """," & _
" [Q12S] = " & """" & .txtQ12 & """," & _
" [Q13S] = " & """" & .txtQ13 & """," & _
" [Q14S] = " & """" & .txtQ14 & """," & _
" [DATE] = " & """" & .txtDate & """" & _
" WHERE tblDevelop.DevID = " & " " & .txtDevID
End With

CurrentDb.Execute strUpdateSQL, dbFailOnError

MsgBox "Thank You"

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top