Error 13 type mismatch

jptaz

New Member
Joined
May 1, 2020
Messages
46
Office Version
  1. 2010
Platform
  1. Windows
Hello everyone,

Here's my problem. I have a userform with textboxes titled Reg 1 to Reg 597.
My full code

VBA Code:
Option Explicit

Dim bFlgExit As Boolean
Dim LocOldLength As Integer
Dim TbxDate() As New Classe1
Dim Tbx7() As New Classe1


Function GetDataType(ByVal Text As String) As Variant
    If IsNumeric(Text) Then
            GetDataType = Val(Text)
      Else
            GetDataType = Text
      End If
End Function


' When we click the 'continue' button
Private Sub CommandButton1_Click()
  Dim Sht As Worksheet, cStart As Range
  Dim LasteRow As Long
  Dim TargetRow As Long 'variable for position control
  Dim FullName As String 'full name
  Dim UserMessage As String 'variable to configure user message at the end
  Dim Ind As Long
  Dim Dossier As String
  
  ' Désactiver les évènements et le calcul auto
  Application.EnableEvents = False
  Application.Calculation = xlCalculationManual
  '
  Dossier = Reg5
  FullName = Reg4 & " " & Reg3 'concatenate first and surname for use in code below
  ' Define the work sheet
  Set Sht = ThisWorkbook.Sheets("Data")
  Set cStart = Sht.Range("Data_Start")
  ' The last row
  LasteRow = Sht.ListObjects("Tableau1").ListRows.Count
  'begin check if in 'edit' or 'add new' mode
  If Sheets("Engine").Range("B4").Value = "NEW" Then 'in 'new' mode
    'begin validation check 'check if name already exists
    If Application.WorksheetFunction.CountIf(Sht.Range("H8:d" & LasteRow), Dossier) > 0 Then
      MsgBox "Le numéro de dossier existe déjà", 0, "Check"
      Exit Sub 'notify user and exit the routine
    End If
    'end validation check
    TargetRow = Sht.ListObjects("Tableau1").Range(LasteRow, 1).End(xlUp).Row - 7 + 1
    UserMessage = " a été ajouté à la base de données" 'configure user message for add new entry
  Else 'in 'edit' mode
    TargetRow = Sheets("Engine").Range("B5").Value 'make variable equal to the value saved in the engine
    UserMessage = " a été modifié" 'configure user message for edit entry
  End If
  '''BEGIN INPUT DATA INTO DATABASE'''
  cStart.Offset(TargetRow, 0).Value = TargetRow 'ref
  cStart.Offset(TargetRow, 1).Value = UCase(Reg4) & " " & Reg3 'Txt_First 'full name 'concatenate
  
  
  For Ind = 1 To 597
  cStart.Offset(TargetRow, 2 + Ind - 1).Value = GetDataType(Controls("Reg" & Ind).Value)
  Next Ind
  
    For Ind = 1 To 23
  cStart.Offset(TargetRow, 599 + Ind - 1).Value = Controls("Text_date" & Ind)
  Next Ind
  

  
  ' Réactiver les évènements et le calcul auto
  Application.EnableEvents = True
  Application.Calculation = xlCalculationAutomatic
  '''END INPUT DATA INTO DATABASE'''
  Unload Data_UF 'close the userform
  MsgBox FullName & UserMessage, 0, "Complété" 'display message box (configured according to mode)
End Sub


Private Sub CommandButton2_Click()
  Unload Data_UF
End Sub


' Utilisation d'un module de classe pour tous les controls contenant le terme "date"
Private Sub UserForm_Initialize()
  Dim Ctl As Control, Ind As Long, Ind7 As Integer
  Ind = 0: Ind7 = 0
  ' Pour chaque control de l'UserForm
  For Each Ctl In Me.Controls
    ' Si son nom contient "date"
    If InStr(1, Ctl.Name, "date", vbTextCompare) > 0 Or _
      InStr(1, "Text_sign_ini,Text_sign_fin", Ctl.Name) > 0 Then
      ' Définir une classe pour ce control
      ' Saisie d'une date de 10 caractères
      Ind = Ind + 1
      ReDim Preserve TbxDate(1 To Ind)
      Set TbxDate(Ind).TbxDate = Ctl
    End If
    ' 2 control avec saisi d'une date de 7 caractères
    If InStr(1, "Reg206,Reg351", Ctl.Name, vbTextCompare) > 0 Then
      Ind7 = Ind7 + 1
      ReDim Preserve Tbx7(1 To Ind7)
      Set Tbx7(Ind7).Tbx7 = Ctl
    End If
  Next Ctl
End Sub

I have this class module to avoid infinite loop

VBA Code:
Option Explicit

Private oldLength As Integer
Private FlgExit As Boolean

Public WithEvents TbxDate As MSForms.TextBox
Public WithEvents Tbx7 As MSForms.TextBox

Private Sub TbxDate_Change()
  ' Empêcher la boucle infinie
  If FlgExit Then FlgExit = False: Exit Sub
  '  Tout est ok
  If oldLength > TbxDate.TextLength Then
    oldLength = TbxDate.TextLength
    Exit Sub
  End If
  ' nb caractères maxi autorisé dans le textbox mettre 8 si tu veux l'année à 2 chiffres
  TbxDate.MaxLength = 10
  '
  If TbxDate.TextLength = 4 Or TbxDate.TextLength = 7 Then
    FlgExit = True
    TbxDate.Text = TbxDate.Text & "/"
  End If
  oldLength = TbxDate.TextLength
End Sub

Private Sub Tbx7_Change()
  ' Empêcher la boucle infinie
  If FlgExit Then FlgExit = False: Exit Sub
  '  Tout est ok
  If oldLength > Tbx7.TextLength Then
    oldLength = Tbx7.TextLength
    Exit Sub
  End If
  ' nb caractères maxi autorisé dans le textbox mettre 8 si tu veux l'année à 2 chiffres
  Tbx7.MaxLength = 7
  '
  If Tbx7.TextLength = 3 Then
    FlgExit = True
    Tbx7.Text = Tbx7.Text & "/"
  End If
  oldLength = Tbx7.TextLength
End Sub

And this piece of code to format the text in Reg206 and Reg351 to this ###/###.
VBA Code:
Private Sub UserForm_Initialize()
  Dim Ctl As Control, Ind As Long, Ind7 As Integer
  Ind = 0: Ind7 = 0
  ' Pour chaque control de l'UserForm
  For Each Ctl In Me.Controls
   
    If InStr(1, Ctl.Name, "date", vbTextCompare) > 0 Or _
      InStr(1, "Text_sign_ini,Text_sign_fin", Ctl.Name) > 0 Then
      ' Définir une classe pour ce control
      
      Ind = Ind + 1
      ReDim Preserve TbxDate(1 To Ind)
      Set TbxDate(Ind).TbxDate = Ctl
    End If
  
    If InStr(1, "Reg206,Reg351,Text", Ctl.Name, vbTextCompare) > 0 Then
      Ind7 = Ind7 + 1
      ReDim Preserve Tbx7(1 To Ind7)
      Set Tbx7(Ind7).Tbx7 = Ctl
    End If
  Next Ctl
End Sub

When I put Reg351 in there alone
VBA Code:
If InStr(1, "Reg351", Ctl.Name, vbTextCompare) > 0 Then
everything works flawlessly. But when I insert Reg206
VBA Code:
If InStr(1, "Reg206, Reg351", Ctl.Name, vbTextCompare) > 0 Then
I get Error 13.

It seems the name "reg206" causes the error because if I name my textbox "textbox1" or to any name between "Reg300 to Reg450", the problem doesn't occur (Reg299 and less the problem happens). I don't get why. I prefer not to change the Name "Reg" because it's easier to import in my database.

Any tips or ideas?

thanx in advance
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
I think your problem control is you don't have a control with the name
"Reg206, Reg351"

you need to change you code to this :
VBA Code:
If ((InStr(1, "Reg206", Ctl.Name, vbTextCompare) > 0) Or InStr(1, "Reg351", Ctl.Name, vbTextCompare) > 0) Then
 
Upvote 0
Thanks for your time, I tried your suggestion, however I get the same error. If I change Reg206 for Reg300 and more, I don't get any error... strange isn't it. I aslo tried changing every control names to eg## and I get the same error with eg206 but not with eg351...

any thoughts?
 
Upvote 0
Whilst I haven't looked at you code in any depth, this line is probably not doing what you think
VBA Code:
If InStr(1, "Reg206,Reg351,Text", Ctl.Name, vbTextCompare) > 0 Then
It will return true for Reg2, Reg20, Reg206, Reg3, Reg35 & Reg351, not to mention the Text part.
 
Upvote 0
Whilst I haven't looked at you code in any depth, this line is probably not doing what you think
VBA Code:
If InStr(1, "Reg206,Reg351,Text", Ctl.Name, vbTextCompare) > 0 Then
It will return true for Reg2, Reg20, Reg206, Reg3, Reg35 & Reg351, not to mention the Text part.
Haha indeed, the ",Text" was not suppose to be there. I didn't know however that every controls Reg2, Reg20, Reg206, Reg3, etc. were included in this code. If I remove the ,text it still doesn't work. However if I write it like that
VBA Code:
If InStr(1, "Reg206,Reg351", Ctl.Name) > 1 Then
it works, but the Reg206 doesn't format to ###/###...
 
Upvote 0
If the control name is Reg206, the InStr will return 1 & is therefore ignored.

You could try
VBA Code:
       If InStr(1, "Reg206,Reg351,", Ctl.Name&",") > 0 Then
 
Upvote 0
If the control name is Reg206, the InStr will return 1 & is therefore ignored.

You could try
VBA Code:
       If InStr(1, "Reg206,Reg351,", Ctl.Name&",") > 0 Then
Yesss thank you so much, it works!! Just had to add a space after "Ctl.Name". What was the problem, if you don't mind.

Thanks again!
 
Upvote 0
I explained the problems in posts 4 & 6 ;)
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,198
Members
453,022
Latest member
RobertV1609

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top