TYPE CONVERSION FAILURE WITH TABLE UPDATE QUERY

Anonymous
2021-10-12T02:24:30+00:00

Hi,

I feel like I'm so close to solving a problem that has vexed me all day. My latest error when I attempt to update a table from an unbound form is a "Type Conversion Error". Here is the code:

DoCmd.RunSQL ("UPDATE [Participant Registration List] SET [Participant Registration List].[ACADEMY COHORT NUMBER]=' & Me.[ACADEMY COHORT NUMBER] & ', " & _

"[Participant Registration List].[COHORT START DATE]=' & Me.[COHORT START DATE] & ', " & _

"[Participant Registration List].[AGENCY NAMES]= '" & Me.[AGENCY NAMES] & "', " & _

"[Participant Registration List].[SEATS ALLOCATED]=' & Me.[SEATS ALLOCATED] & ', " & _

"[Participant Registration List].[EMPLOYEE ID NUMBER]= '" & Me.[EMPLOYEE ID NUMBER] & "', " & _

"[Participant Registration List].[EMPLOYEE LAST NAME]= '" & Me.[EMPLOYEE LAST NAME] & "', " & _

"[Participant Registration List].[EMPLOYEE FIRST NAME]= '" & Me.[EMPLOYEE FIRST NAME] & "', " & _

"[Participant Registration List].[EMPLOYEE HR TITLE]= '" & Me.[EMPLOYEE HR TITLE] & "', " & _

"[Participant Registration List].[EMAIL ADDRESS]= '" & Me.[EMAIL ADDRESS] & "', " & _

"[Participant Registration List].[PHONE NUMBER]= '" & Me.[PHONE NUMBER] & "', " & _

"[Participant Registration List].[LOS CITY]= '" & Me.[LOS CITY] & "', " & _

"[Participant Registration List].[TIME IN HR]= '" & Me.[TIME IN HR] & "', " & _

"[Participant Registration List].[GROUP]= '" & Me.[GROUP] & "', " & _

"[Participant Registration List].[AGENCY ABBREV]= '" & Me.[AGENCY ABBREV] & "'")

I think I understand that the type conversation error means that the code as written does not distinguish a text field from a number field from a datetime field. All three types are referred to in the above ("ACADEMY COHORT NUMBER" = NUMBER FIELD, "COHORT START DATE" = DATETIME FIELD, "SEATS ALLOCATED" = NUMBER FIELD, ALL OTHER FIELDS ARE 'SHORT TEXT' FIELDS) . . . but I don't know how to distinguish them. Please Please help.

I'd be so grateful to get some sleep tonight :)

Microsoft 365 and Office | Access | For business | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

39 answers

Sort by: Most helpful
  1. Anonymous
    2021-10-15T23:14:43+00:00

    btw...thank you for the tip. I will bookmark Allen Brown's tips and articles for future.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2021-10-15T23:06:41+00:00

    With that in mind, Tom, what do you think of this:

    On Error GoTo ProcError

    Dim db As DAO.Database

    Dim StrSQL As String

    Set db = CurrentDb()

    StrSQL = "UPDATE [Participant Registration List]" & _

    "SET [ACADEMY COHORT NUMBER]=" & Me.[ACADEMY COHORT NUMBER] & ", " & _

    "[COHORT START DATE]= #" & Me.[Cohort Start Date] & "#, " & _

    "[AGENCY NAMES]= '" & Me.[AGENCY NAMES] & "', " & _

    "[SEATS ALLOCATED]= " & Me.[SEATS ALLOCATED] & ", " & _

    "[EMPLOYEE ID NUMBER]= '" & Me.[EMPLOYEE ID NUMBER] & "', " & _

    "[EMPLOYEE LAST NAME]= " & Chr(34) & Me.[EMPLOYEE LAST NAME] & ", " & _

    "[EMPLOYEE FIRST NAME]= '" & Me.[EMPLOYEE FIRST NAME] & "', " & _

    "[EMPLOYEE HR TITLE]= '" & Me.[EMPLOYEE HR TITLE] & "', " & _

    "[EMAIL ADDRESS]= '" & Me.[EMAIL ADDRESS] & "', " & _

    "[PHONE NUMBER]= '" & Me.[PHONE NUMBER] & "', " & _

    "[LOS CITY]= '" & Me.[LOS CITY] & "', " & _

    "[TIME IN HR]= '" & Me.[TIME IN HR] & "', " & _

    "[GROUP]= '" & Me.[GROUP] & "', " & _

    "[AGENCY ABBREV]= '" & Me.[AGENCY ABBREV] & "'" & _

    WHERE(([Participant Registration List].[Agency Cohort Number] Is Null) And ([Participant Registration List].[Cohort Start Date] Is Null))

    Debug.Print StrSQL

    db.Execute StrSQL, dbFailOnError

    MsgBox db.RecordsAffected & "records updated in Participant Registration List table.", vbInformation, "Records Successfully Updated..."

    ExitProc:

    Set db = Nothing

    Exit Sub

    ProcError:

    MsgBox "Error" & Err.Number & ":" & Err.Description, _

    vbCritical, "Error in db.execute"

    Resume ExitProc

    End Sub

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2021-10-15T23:06:21+00:00

    Hello, lmb,

    :
    
    :
    

    Do you mind detailing what your alterative would look like? I'm sure it would help me tremendously.

    I think lmb is referencing his answer, shown indented, below, to you in this post: 10/13/2021 1:06:17 AM

    https://answers.microsoft.com/en-us/msoffice/forum/all/runtime-error-3134-syntax-error-in-insert-into/f24d7ccd-ef7c-4e36-9ab2-6579c2bca5ee 

    Notice how he is using a recordset. My only advice here is to be careful to prevent Error 13: Type Mismatch, by being explicit in the declaration of the type of recordset: A DAO recordset or an ADO recordset? You really don't want code that compiles fine, but blows up with a run-time error, simply because an ADO reference has higher priority than a DAO reference, when viewed by clicking on Tools > References. More information here:

    ADO and DAO Library References in Access Databases

    http://www.accessmvp.com/TWickerath/articles/adodao.htm 

    Here is lmb's previous reply:

    Hi toshikatae,

    As an alternative you could also use something like this:

    Private Sub Command131_Click()

    Dim cur_set As Recordset

    Set cur_set = CurrentDb.Openrecordset("SELECT * FROM [Participant Registration List] WHERE Participant_id = 0")

    With cur_set

    .AddNew

    ![Academy Cohort Number] = Me![Academy Cohort Number]

    ![Cohort Start Date] = Me![COHORT START STATE]

    ![Seats Allocated] = Me![Seats Allocated]

    ![Employee ID Number] = Me![Employee ID Number]

    ![Employee Last Name] = Me![Employee Last Name]

    ![Employee First Name] = Me![Employee First Name]

    ![Employee HR Title] = Me![Employee HR Title]

    ![Email Address] = Me![Email Address]

    ![Phone Number] = Me![Phone Number]

    ![LOS City] = Me![LOS City]

    ![Time in HR] = Me![Time in HR]

    ![Group] = Me![Group]

    ![Agency Abbrev] = Me![Agency Abbrev]

    .Update

    End With

    End Sub

    Imb.

    Whether you use multiple SQL statements, each with a call to db.Execute, or multiple recordset operations is "six of one, half a dozen of the other". Personally, to me, a SQL statement that can be printed to the immediate window, then copied and run in a new query (paste into SQL View), is easier to troubleshoot if there is later an error.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2021-10-15T23:05:41+00:00

    LOL. Luckily (at least for me), Tom, I "only" want to append (add) data to the end of each field.

    mc

    Was this answer helpful?

    0 comments No comments
  5. Anonymous
    2021-10-15T22:45:44+00:00

    First BIG "Thank You" to both of you for your help. It embarrasses me to know so little about what I'm doing, but I am gratified there are people willing to lend a hand. I'm currently working through everything you've said and re-vising the code. Tom, I've corrected #1 and see the small quotation marks you referenced in red. As far as the other recommendations: #2) Made the correction. thank you for finding the spacing error. Unchecked the Background Compile and Compile on Demand, #3) Yes, it was intentional not to include [Course Year] in the strSQL. Hope this won't cause any problems? #4) Understood the logic behind including 'Ch(34)' thank you, and #5) Eliminated the [table name].qualifiers for the reasons you stated.

    NOW . . . for the WHERE statement. I may not be thinking of this properly, but since the updated data would be added to the end of every field in a dataset, should the WHERE statement be something like:

    WHERE [Academy Cohort Number] Is Null

    AND [Cohort Start Date] Is Null

    AND [Agency Names] is Null

    ' repeated for each field in the table?

    Would that work? Probably not, but I'd appreciate your recommendation.

    You are very welcome! Please don't be embarrassed about being at the bottom of a learning curve, looking up. We've all been there at one time!

    #3) Yes, it was intentional not to include [Course Year] in the strSQL. Hope this won't cause any problems?

    It won't cause any problem, as long as having a NULL (or ZLS = Zero Length String) is considered okay.

    By the way, I typically reset the default property for text and memo (e.g. "Short Text" and "Long Text") columns from Allow Zero Length: Yes to Allow Zero Length: No You might ask why? I will point you to Allen Browne's article on this topic. Allen was a long time Access MVP (about 17 years, I think), before he decided to pursue his interest in the ministry. Anyway, if you want to learn Access, I suggest reading every tip and article that Allen has published. Just set a goal of doing maybe 5 per week.

    Flaws in Microsoft Access

    http://allenbrowne.com/bug-09.html 

    NOW . . . for the WHERE statement. I may not be thinking of this properly, but since the updated data would be added to the end of every field in a dataset, should the WHERE statement be something like:

    Are you wanting to append (add) data to the end of each field, or replace the entire contents?

    In other words, let's say your GROUP value was = FBI. If you want to update it to CIA, which result are you after:

    1. CIA
    2. FBI, CIA

    Hopefully, your answer is NOT #2. If it is, it is time to re-read the database normalization papers I provided earlier. But if you were thinking that, fear not: there is no reason for feeling embarrassed.

    Tom

    Was this answer helpful?

    0 comments No comments