Wrox Programmer Forums
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA 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 February 3rd, 2004, 05:30 PM
Authorized User
 
Join Date: Jan 2004
Posts: 43
Thanks: 0
Thanked 0 Times in 0 Posts
Default run-time error 3071

Hi,

I am using a query to insert records into a history table. The code below loops through my recordset. It worked before, but all of a sudden produces the following error on the rec.MoveNext line.

Run-time error 3071. This expression is typed incorrectly, or it is too complex to be evaluated. For example, a numeric expression may contain too many complicated elements. Try simplifying the expression by assigning parts of the expression to variables.

I tried to put the insert statement into a variable but that didn't work. here is the while statement I'm using. Any help would be greatly appreciated.

While Not rec.EOF
        DoCmd.RunSQL "INSERT INTO HISTORY (BLI, BLI_DESCR, AUTH, FREEZE_DATE, APPEND_DATE) VALUES ('" & rec("BLI") & "', '" & rec("BLI_DESCR") & "', '" & rec("AUTH") & "', '" & rec("FREEZE_DATE") & "', DATE())"
        rec.MoveNext
        Count = Count + 1
    Wend
 
Old February 3rd, 2004, 08:40 PM
Authorized User
 
Join Date: Jun 2003
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

At a wild guess I would look to see if you are posibly violating a primary key in your table or posibly passing null values.

Thanks,
Jesse
 
Old February 4th, 2004, 09:34 PM
Authorized User
 
Join Date: Oct 2003
Posts: 75
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You may also want to display your SQL statement after it's been built in your debug window. One of the most common errors is when apostrophes get embedded in your data values. I actually got this function from a Sam's book years ago - it looks something like this:

Public Function adhHandleQuotes(ByVal varValue As Variant, _
                                Optional strDelimiter As String = "'") As Variant

    adhHandleQuotes = _
     strDelimiter & _
     Replace(varValue, strDelimiter, strDelimiter & strDelimiter) & _
     strDelimiter

End Function

As can be seen above, the second parameter (strDelimiter) is optional and defaults to a single quote. Using string concatenation for readability, here's how it would look using the new function:

Dim strSQL As String

While Not rec.EOF
   strSQL = ""
   strSQL = strSQL & "INSERT INTO HISTORY "
   strSQL = strSQL & "(BLI, BLI_DESCR, AUTH, FREEZE_DATE, APPEND_DATE) "
   strSQL = strSQL & "VALUES ( "
   strSQL = strSQL & adhHandleQuotes(rec("BLI")) & ", "
   strSQL = strSQL & adhHandleQuotes(rec("BLI_DESCR")) & ", "
   strSQL = strSQL & adhHandleQuotes(rec("AUTH")) & ", "
   strSQL = strSQL & adhHandleQuotes(rec("FREEZE_DATE"), "'") & ", "
   strSQL = strSQL & adhHandleQuotes(Date(), "'")
   strSQL = strSQL & ")"
   DoCmd.RunSQL strSQL
   rec.MoveNext
   Count = Count + 1
Wend

I included the second function argument for the FREEZE_DATE and APPEND_DATE in case the date delimiter is a single quote instead of a pound sign.

I use the function for ALL of my string-related processing when inserting new or updating existing rows in a table.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert Query Error & Run-Time Error 3022 DavidWE Access 1 July 31st, 2008 11:17 AM
run-time error(s) Chacko C++ Programming 0 March 4th, 2007 02:28 PM
run time error ashishroyk Java GUI 0 October 8th, 2004 01:42 AM
Run Time Error JBond Access VBA 0 May 27th, 2004 09:50 AM
RUN-TIME ERROR compcad Beginning VB 6 2 May 21st, 2004 02:01 AM





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