Mike
Mike

Reputation: 1011

MS Access VBA: Getting Error '13': Type Mismatch when calling a function from a different form

Edit: problem has been solved. See my answer in a separate post below!

I have two forms: Calendar and frmMagnet. When a text box in Calendar is clicked, it opens frmMagnet and is supposed to call a function declared in frmMagnet. But when I click on the text box, I get Error 13.

Global Vars

Private CalendarArray(42, 2) As Variant

CalenderArray initialization

Private Sub InitVariables()
  intMonthSelect = Month(CDate(CStr(Me.MonthComboBox) & " 1"))
  intYearSelect = Me.YearComboBox
  lngDate = CLng(DateSerial(intYearSelect, intMonthSelect, 1))
  strUnscheduledJobs = ""
  'Initialize CalendarArray
  Dim i As Integer
  For i = 0 To UBound(CalendarArray) - 1
      CalendarArray(i, 0) = lngDate - Weekday(lngDate) + 1 + i
      CalendarArray(i, 1) = CStr(Day(CalendarArray(i, 0)))
  Next i
End Sub

Calendar method opening the magnet form

Private Sub text1_DblClick(Cancel As Integer)
  If Len(Me.ActiveControl.Text) > 2 Then
    Call OpenTextBox(Me.ActiveControl.Name)
  End If
End Sub

Private Sub OpenTextBox(ctlName As String)
  Dim ctlValue As Integer
  Dim DayOfMonth As Long

  ctlValue = Me.Controls(ctlName).Tag
  DayOfMonth = CalendarArray(ctlValue - 1, 0)
  DoCmd.OpenForm "frmMagnet"

  Call Forms("frmMagnet").PopulateHeaderText(DayOfMonth) 'THE ERROR IS ON THIS LINE!!!!!
End Sub 

frmMagnet method

Public Sub PopulateHeaderText(theDate As Long)
  Me.Controls(HeaderText) = CStr(theDate)
End Sub

Thank you!

Upvotes: 1

Views: 2698

Answers (2)

Mike
Mike

Reputation: 1011

So my problem was

Me.Controls(HeaderText) = CStr(theDate)

needed quotations around HeaderText. The correct syntax is

Me.Controls("HeaderText") = CStr(theDate)

Upvotes: 1

Mark Horner
Mark Horner

Reputation: 118

On your method opening the magnet form you have this line of code;

 ctlValue = Me.Controls(ctlName).Tag

ctlValue is Long, are all of your fields tagged properly without any string characters (and no nulls)?

My advice here, is set a breakpoint (go into VBA and click in the bar on the left to get a red dot) at the start of the procedure. Then press your button to run the procedure, and step by step through until you find the offending line (use F8 to go step by step).

Lastly, it's worth turning on option explicit and declaring all your variables properly, it helps massively in this situation too.

Upvotes: 1

Related Questions