Yotam
Yotam

Reputation: 10485

Pointing at Access controls, Visual Basic

My function:

Public Function openRpt(strReportName As String, form as ??, subform as ??)
    On Error Resume Next
    If (Forms![form]![subform].Form.lock = False) Then
        DoCmd.RunCommand acCmdSaveRecord
    End If
    DoCmd.OpenReport strReportName, acViewPreview, "", _
        "[num]=Forms![form]![subform].Form.[num]"
End Function

What it does is to save a record from subform if it isn't locked, and then launch a report. I want to switch from and subform with matching variables that will point at the right forms, and will be given as arguments.

  1. What type of variable should I use?
  2. What is the difference between using '!' and '.' to access properties? I understand that '!' is used to access a controller and '.' is used to access a table record, am I right?

Upvotes: 0

Views: 126

Answers (1)

SeanC
SeanC

Reputation: 15923

for (1):
First, it may be tempting to call variables form and subform, but watch out for possible errors if names you use match or are close to reserved names.
Also, lock is not a valid property of a form or subform. If you are trying to test if a form has been changed, you would test .Dirty This is how I would code your function:

Option Explicit
Public Function openRpt(strReportName As String, frm as String, sFrm as string)
    If (Forms(frm).Form(sFrm).Form.Dirty = True) Then
        DoCmd.RunCommand acCmdSaveRecord
    End If
    DoCmd.OpenReport strReportName, acViewPreview, "", _
        "[num]=Forms(""" & frm & """).Forms(""" & sFrm & """).Form.[num]"
End Function

for (2): from the Microsoft Blog

  • The dot gives you early binding and is resolved at compile time, the bang is resolved at runtime.
  • In the case of Forms, both controls and fields in the underlying query can be referenced via a dot since they are all in the type library for the form.
  • Also in the case of Forms, if you change the underlying query at runtime - dot references to the old query's fields will fail at runtime since update of the Form's type library cannot happen at runtime.
  • Because the dot is early bound, IntelliSense happens by default with the dot, not with the bang.
  • The dot (since it is early bound) is faster than the bang, but no one runs enough code that uses these items enough for the performance difference to actually matter.

Upvotes: 2

Related Questions