How to avoid name error in ms access formula? - ms-access

I want to set DefaultValue of a form field to
=IIf(IsNull([Forms]![MyForm]![MySubForm].[Form]![MyField]);"";[Forms]![MyForm]![MySubForm].[Form]![MyField])
When MyForm is closed I want to set the field value to "". Unfortunately I can't find how to test if MyForm is closed or not : I have tried IsNull, IsObject, very Is it always return #Name?
Is this possible ?

I use a function like this to check to see if a form is open:
Function IsLoaded(ByVal strFormName As String) As Boolean
' Returns True if the specified form is open in Form view or Datasheet view.
Const conObjStateClosed = 0
Const conDesignView = 0
If SysCmd(acSysCmdGetObjectState, acForm, strFormName) <> conObjStateClosed Then
If Forms(strFormName).CurrentView <> conDesignView Then
IsLoaded = True
End If
End If
End Function

Related

MS Access using function on onclick event of button

I have below function on form:
Function StringsHaveAMatchingCharacter(String1 As Variant, String2 As Variant) As Boolean
' Return True if the two strings have at least one character in common;
' otherwise return False.
Dim lngX As Long
If IsNull(String1) Or IsNull(String2) Then
' No point checking for a match.
Else
For lngX = 1 To Len(String1)
If InStr(String2, Mid$(String1, lngX, 1)) <> 0 Then
StringsHaveAMatchingCharacter = True
Exit Function
End If
Next lngX
End If
StringsHaveAMatchingCharacter = False
End Function
I can call this function with commandbutton on form. But how to use specify on my event if function returns "false" or "true" as I want new dialog form to be open if function returns "True" else I want msgbox and cancel opening dialog form.
Not sure I understand the problem. You simply call the function and use its return value.
Private Sub cmdOpenDialog_Click()
Dim isMatch As Boolean
isMatch = StringsHaveAMatchingCharacter(Me!Text1.Value, Me!Text2.Value)
If isMatch Then
DoCmd.OpenForm "myDialog"
Else
MsgBox "Nope."
End If
End Sub

MS Access Query : Check if form is open / Check if parameter requires input

I'm currently trying, in a MS Access Query (using the GUI tool, and not the SQL tool), to check if a specific form is open or not, without using VBA.
Using the following expression :
Expr1 : [Formulaires]![Form1].[Visible]
If Form1 is open, the query works fine, but if Form1 is closed, it asks for input, since the Form1 isn't open, and the variable doesn't exist anymore.
Is there a way to check if a specific form is open, or to check if a certain variable requires input ?
You would need a VBA function to discover this but you could call the function from your query and incorporate the result in your formula.
This function will return true if the form is loaded;
Function IsFormLoaded(strForm As String) As Boolean
Dim frm As Form
For Each frm In Forms
If frm.Name = strForm Then
IsFormLoaded = True
Exit Function
End If
Next
End Function
You can then use the function in an IIF statement;
IIF(IsFormLoaded('MyForm'), MyForm.Control.Value, '')
Yes, use a function:
Public Function IsFormLoaded(ByVal strForm As String) As Boolean
Dim frmForm As Form
Dim booLoaded As Boolean
For Each frmForm In Forms
If frmForm.Name = strForm Then
booLoaded = True
Exit For
End If
Next
IsFormLoaded = booLoaded
Set frmForm = Nothing
End Function
I use this:
If IsOpen("FormName") Then
DoCmd.Close acForm, "FormName"
End if
[put this function in a separate module]
Public Function IsOpen(fn As String) As Boolean 'returns True if form is open
If SysCmd(acSysCmdGetObjectState, acForm, fn) <> 0 Then IsOpen = True
End Function

Using Passed Variable to Select Items like "Me.myvar.Item"

I am trying to create a new function that I can pass variables to and those variables will be used to select and modify items.
Function
Function TabDisplay(Switch As String, TargetTab As String)
If (Me. & Switch & .Value) = -1 Then
Me.Tabs.Pages.Item("TargetTab").Visible = True
ElseIf Me.Switch.Value = 0 Then
Me.Tabs.Pages.Item(TargetTab).Visible = False
Else
Me.Switch.Value = "0"
End If
End Function
How I call the function
TabDisplay "SoftwareInstallable", "tabSoftware"
Working Code that I used before attempting this function
If Me.SoftwareInstallable.Value = -1 Then
Me.Tabs.Pages.Item("tabSoftware").Visible = True
ElseIf Me.SoftwareInstallable.Value = 0 Then
Me.Tabs.Pages.Item("tabSoftware").Visible = False
Else
Me.SoftwareInstallable.Value = "0"
End If
====Final Code====
Function
Function TabDisplay(Switch As String, TargetTab As String)
If Me.Controls(Switch).Value = -1 Then
Me.Tabs.Pages.Item(TargetTab).Visible = True
ElseIf Me.Controls(Switch).Value = 0 Then
Me.Tabs.Pages.Item(TargetTab).Visible = False
Else
Me.Controls(Switch).Value = "0"
End If
End Function
Function Call
TabDisplay "SoftwareInstallable", "tabSoftware"
I think I understand what you want. But let me describe it with a different example.
I have a form with a text box named txtFoo. So I can Debug.Print its value to the Immediate window like this ...
Debug.Print Me!txtFoo.Value
Later I decide I want to store the control name in a variable so that I can Debug.Print the value of any other control simply by changing the variable.
Dim strControlName As String
strControlName = "txtBar"
Then I can use that variable to reference the matching item in the form's Controls collection, and obtain its value.
Debug.Print Me.Controls(strControlName).Value
I think you can use that approach to accomplish what you want, but I won't attempt to rewrite your code sample because I suspect I would get it wrong.

OldValue and Value same on radio button change Access 2007 VB

I have an access 2007 front-end app. On a particular form, there are 2 radio buttons in a radio button group. I am trying to detect when the radio button group is changed and capture the old and new values, but my OldValue and Value properties are = in the save event, even if I have changed it. The OldValue is equal to the New radio button value, not what it was originally.
I tried coding this in the form's Save subroutine. The intent was to compares the RB value with the original dataset value to force setting the old value, but it doesn't like the 'SET' statements
If fraResistOption.Value = 1 And (IsNull([Dl_Resisted]) Or UCase([Dl_Resisted]) = "N") Then
Set fraResistOption.OldValue = 1
[Dl_Resisted] = "N"
Else
If fraResistOption.Value = 1 And (Not IsNull([Dl_Resisted]) Or UCase([Dl_Resisted]) = "Y") Then
Set fraResistOption.OldValue = 2
[Dl_Resisted] = "N"
Else
If fraResistOption.Value = 2 And (IsNull([Dl_Resisted]) Or UCase([Dl_Resisted]) = "N") Then
Set fraResistOption.OldValue = 1
[Dl_Resisted] = "Y"
Else
If fraResistOption.Value = 1 And (Not IsNull([Dl_Resisted]) Or UCase([Dl_Resisted]) = "Y") Then
Set fraResistOption.OldValue = 2
[Dl_Resisted] = "Y"
End If
End If
End If
End If
Could someone suggest a way to do this? Please and thank you.
The .OldValue property of the Option Group (sometimes referred to as "Frame") does work. I have a table named [optValues]:
[ID] - AutoNumber
[optValue] - Numeric (Long Integer)
It contains one record:
ID optValue
1 3
My form's Record Source is the [optValues] table. The form has an Option Group named "Frame0" whose Control Source is the [optValue] field. It contains three Option buttons
label: "foo", value: 1
label: "bar", value: 2
label: "baz", value: 3
The After Update event handler for Frame0 is:
Private Sub Frame0_AfterUpdate()
MsgBox "Old value: " & Me.Frame0.OldValue & ", New value: " & Me.Frame0.Value
End Sub
When I open the form, "baz" is selected (because [optValue] is 3 in the table):
When I click on "foo" I immediately see the (correct) old and new values:
The only way I can think of to detect and capture changes to Option-Group values is to handle the Form_Current event (save the Option Group value), then also handle the Option-Group After event. Although you can change the Option-Group.Value, OldValue is likely a protected (read-only) property. Hope something like the following helps:
Dim OldValue As Byte
Dim CurrentValue As Byte
Private Sub Form_Current()
OldValue = Frame0.Value
End Sub
Private Sub Frame0_AfterUpdate()
CurrentValue = Frame0.Value
Debug.Print "AFTER: OldValue=" & OldValue & "' CurrentValue=" & CurrentValue
End Sub
I don't know why but .oldvalue doesn't work for me.
If you are in the same boat as me, you can use the BeforeUpdate event of the optionGroup and set a static variable.
Then read the static variable in afterUpdate event and reset it for the next change.

How to create a new form instance using the name of the form as a String

Code to create new form instance of a closed form using form name
I want to replace the long Select Case list with a variable.
Full code of module
In Access 2010 I have a VBA function that opens a new instance of a form when given a string containing the form's name. By adding a form variable "frm" to a collection:
mcolFormInstances.Add Item:=frm, Key:=CStr(frm.Hwnd)
The only way I can figure out to open "frm" is with a Select Case statement that I've manually entered.
Select Case strFormName
Case "frmCustomer"
Set frm = New Form_frmCustomer
Case "frmProduct"
Set frm = New Form_frmProduct
... etc ... !
End Select
I want it to do it automatically, somewhat like this (although this doesn't work):
Set frm = New Eval("Form_" & strFormName)
Or through some code:
For Each obj In CurrentProject.AllForms 'or AllModules, neither work
If obj.Name = strFormName Then
Set FormObject = obj.AccessClassObject 'or something
End If
Next
Set frm = New FormObject
I just want to avoid listing out every single form in my project and having to keep the list updated as new forms are added.
I've also done some testing of my own and some reading online about this. As near as I can tell, it isn't possible to create a new form object and set it to an instance of an existing form using a string that represents the name of that form without using DoCmd.OpenForm.
In other words, unless someone else can prove me wrong, what you are trying to do cannot be done.
I think you are looking for something like this MS-Access 2010 function. (The GetForm sub is just for testing):
Function SelectForm(ByVal FormName As String, ByRef FormExists As Boolean) As Form
For Each f In Application.Forms
If f.Name = FormName Then
Set SelectForm = f
FormExists = True
Exit Function
End If
Next
FormExists = False
End Function
Sub GetForm(ByVal FormName As String)
Dim f As New Form
Dim FormExists As Boolean
Set f = SelectForm(FormName, FormExists)
If FormExists Then
MsgBox ("Form Found: " & f.Caption)
Else
MsgBox ("Form '" & FormName & "' not found.")
End If
End Sub
Here's an ugly hack I found:
DoCmd.SelectObject <acObjectType>, <YourObjectsName>, True
DoCmd.RunCommand acCmdNewObjectForm
The RunCommand step doesn't give you programmatic control of the object, you'll have to Dim a Form variable and Set using Forms.Item(). I usually close the form after DoCmd.RunCommand, then DoCmd.Rename with something useful (my users don't like Form1, Form2, etc.).
Hope that helps.