Results 1 to 8 of 8
  1. #1
    jbeets is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Feb 2020
    Posts
    94

    Type Mismatch error when calculating value based on comboboxes and populating textbox

    Hello,
    I have a form which has several comboboxes with the dropdown options of "N/A, 4,3,2,1". I have a text box (txtScore) which should populate when the user presses the btnCalculate. I am getting a Type Mismatch error however and I cannot figure out what the issue is.

    In the table that the form is connected to the fields with the comboboxes are formatted as Short Text but I have also tried Number. The txtScore field is formatted as Number.

    The code I am using I currently use in an Excel Userform and it works there. So maybe everything does not transfer over exactly in Access? This is the piece that highlights with the Type Mismatch error:
    control_value = control_value + CInt(target_control.Value)

    Any help would be greatly appreciated!
    Code:
    Private Sub btnCalculate_Click()
    Dim target_control As Control
    Dim control_value As Integer
    Dim control_count As Integer
    control_value = 0
    control_count = 0
    
    For Each target_control In Me.Controls
        If TypeName(target_control) = "ComboBox" Then
            If Not target_control.Value = "N/A" Then
                control_value = control_value + CInt(target_control.Value)
                control_count = control_count + 1
            End If
        End If
    Next
    txtPotential = control_count * 4
    txtScore = Format((control_value / txtPotential), "Percent")
    
    For Each c In Me.Controls
        If TypeName(target_control) = "ComboBox" Then
            If target_control.Value = "" Then na = MsgBox("All fields must be selected. Please choose 1-4 or N/A before continuing.", vbOKOnly, "Quarterly Audits")
        End If
    Next
    End Sub


  2. #2
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,471
    Looks like it is not recognizing the "N/A" and dropping to that calculation. Maybe try for the line about it:

    If target_control <> "N/A" then

    You could put a breakpoint on that line and mouse over the fields to see what it is showing.

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Hit debug when you get the error and hover over

    target_control.Value

    to see what its value is. If it's text or Null you'll get that error.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Geez, talk about slow typing.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,407
    Just to make sure something is in the combobox:

    Code:
     If Not target_control.Value = "N/A" Or Not IsNull(target_control.Value) Then
    Last edited by davegri; 02-21-2020 at 03:37 PM. Reason: I'm slow too!

  6. #6
    jbeets is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Feb 2020
    Posts
    94
    Hey Bulzie - Thank you for your quick response! I found the issue but am not 100% sure what I need to add in to fix it....On this form I have comboboxes that should not be apart of the calculation and are not numeric values. cboStatus and cboEmployee. How can I add into the code to ignore those comboboxes?

    Thank you again for helping me!

  7. #7
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,471
    Would think maybe there is an option like target_control.Name that will tell you the name of the control so you can exclude the combo boxes you don't want.

    If target_control.name <> "cboStatus" and
    target_control.name <> "cboEmployee" then

    Code....

    end if

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Type 13 (Type Mismatch) error
    By sdel_nevo in forum Programming
    Replies: 5
    Last Post: 01-22-2016, 10:01 AM
  2. Replies: 5
    Last Post: 10-27-2014, 10:33 PM
  3. Replies: 7
    Last Post: 11-16-2012, 03:02 PM
  4. Replies: 1
    Last Post: 05-11-2012, 10:59 AM
  5. Date - Textbox type mismatch 13
    By mdex in forum Programming
    Replies: 7
    Last Post: 01-20-2012, 08:44 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums