Button to turn change AllowEdits to true in Access using VB - ms-access

I am trying create a button which changes the value of AllowEdits to False and another for true for a subform. I am using the below code. I get a Runtime error 424 each time I run it.
Option Compare Database
Private Sub Toggle_Edit_Click()
Dim strForm As String
strFormName = Me.Name
Call ToggleEdit(Me)
End Sub
and
Option Compare Database
Public strFormName As String
Sub ToggleEdit(myForm As Form)
Call Message
ctrlControl.AllowEdits = True
End Sub
and if you were interested
Sub Message()
MsgBox "Remember not to overwrite incorrect records"
End Sub

Please add Option Explicit at top of your modules!
I think AllowEdits is a Form property, not a Control property.
Option Explicit
Sub ToggleEdit(myForm As Form)
myForm.AllowEdits = Not myForm.AllowEdits
End Sub
If the code is behind the form itself, you can use Me.
Sub ToggleEdit() 'no parameter
Me.AllowEdits = Not Me.AllowEdits
End Sub
If you want to act at control level, use Locked or Enabled properties.

Related

Access Listbox AfterUpdate Not Firing when Selected = True

I have an Access form with a listbox and a combo box.
Private Sub Form_Open(Cancel As Integer)
'... misc code
me.comboBox.RowSource = "sql statement"
call comboBox_AfterUpdate
End Sub
Private Sub comboBox_AfterUpdate()
listbox.Selected(0) = true
End Sub
Private Sub listbox_AFterUpdate()
'.... important code that updates the form
End Sub
On the form load/open, the the code runs as
Form_Open > comboBox_AfterUpdate > listbox_AfterUpdate
However after the form has already loaded, when the combo box is changed and the comboBox_AfterUpdate is triggered, the listbox_AfterUpdate() is NOT triggered.
I would move the code in the listbox_AfterUpdate() to a separate sub-routine, but that would call the code to run twice when the form loads. Any ideas on why this happens?
Similar issue here:
http://www.tek-tips.com/viewthread.cfm?qid=1395160
You could use two custom functions and use optional parameters to know the context in which it is called. That way you have full control over when which function does which.
E.g.
Public Sub MyFunc1 (Optional CalledFromLoad As Boolean)
Public Sub MyFunc2 (Optional CalledFromLoad As Boolean)
Private Sub comboBox_AfterUpdate()
Call MyFunc1 False
End Sub
Private Sub listbox_AFterUpdate()
Call MyFunc2 False
End Sub
Private Sub Form_Open(Cancel As Integer)
Call MyFunc1 True
End Sub
Moving all your code gives you a greater degree of customization, and can help you avoid calling the same code twice.
You could possibly set a global variable in MyFunc1, then trigger MyFunc2 by changing the listbox, and then reset that global variable again. Numerous solutions are possible.

get Access VBA have a Sender

In Access I have a form that has about 200 text boxes on it. I Know that in C# on every on click event there is textbox1_Click(object sender).
how can I in VBA have reference the current sender without saying Me.txt_Whatever?
this is what I have so far
Private Sub txtHotMix_DblClick(Cancel As Integer, sender As Object)
Dim txt As TextBox
Set txt = sender
MsgBox txt.Name
End Sub
I think the ActiveControl property of the Screen object is what you're after (https://msdn.microsoft.com/en-us/library/bb225491(v=office.12).aspx):
In a module:
Option Compare Database
Option Explicit
Public Sub ctrl_Print(objCtrl As Control)
MsgBox objCtrl.Value, vbOKOnly, "Control Value"
End Sub
You would still have to add a click event to every control to call this sub, like this:
Option Compare Database
Option Explicit
Private Sub Text0_Click()
Call ctrl_Print(Screen.ActiveControl)
End Sub

Use a button to Enable/Disable a text box in VBA

I have a form with a text box named Contract_Applying_for which is disabled on form load, but I want to have a button which allows me to edit the contents of the text box.
When I add a button I get presented with the Command Button Wizard, so I have created a Macro called ToggleEnableButton which has the instruction to
RunCode Function Name "=ToggleEnableButton()"
Then I have written the function
Function ToggleEnableButton()
If Me.Contract_Applying_for.Enabled = True Then
Me.Contract_Applying_for.Enabled = False
Else
Me.Contract_Applying_for.Enabled = True
End If
End Function
This seems to produce the error "Member already exists in an object module from which this object module derives."
The code for the ToggleEnableButton_Click is automatically created by the Command Button Wizard and is
Private Sub ToggleEnableButton_Click()
On Error GoTo Err_ToggleEnableButton_Click
Dim stDocName As String
stDocName = "ToggleEnableMacro"
DoCmd.RunMacro stDocName
Exit_ToggleEnableButton_Click:
Exit Sub
Err_ToggleEnableButton_Click:
MsgBox Err.Description
Resume Exit_ToggleEnableButton_Click
End Sub
Any suggestion of what I am doing wrong or a better way to approach this.
Seems like a very simple thing that I am trying to do, but quite a long winded approach.
As suggested by Peekay in the comments I have tried to use a checkbox instead, I wrote
Private Sub chbToggleEdit_Click()
If Me.chbToggleEdit.Value = False Then
Me.Contract_Applying_for.Enabled = False
Else
Me.Contract_Applying_for.Enabled = True
End If
End Sub
This gives the error: "A problem occurred while Microsoft Access was communication with the OLE server or ActiveX Control."
Can you not simply have:
Private Sub ToggleEnableButton_Click()
On Error GoTo Err_ToggleEnableButton_Click
ToggleEnableButton()
Exit_ToggleEnableButton_Click:
Exit Sub
Err_ToggleEnableButton_Click:
MsgBox Err.Description
Resume Exit_ToggleEnableButton_Click
End Sub
You can declare your function with the code behind your form.
Let me know if that works,
Ash

Opening a zoom window in MS Access

I am having an issue with opening a zoom window in a sub form.
Basically, I have created a pop up window (form) which is suppose to appear upon double clicking in a memo field in a sub form to allow the user to be able to zoom in on the field and have additional ease upon entering long sentences.
I believe the problem is linked to the fact that the form which I am trying to create the zoom window is actually a sub form embedded in a form. My reasoning behind this is due to the fact that my code works perfectly well when i open the sub form alone and double click on the zoom in field..
Below is the code. The subform name is 'frmMasterListOfEventsDetails", the control / field to zoom in on in the sub form is called "notes2". The pop up window (subform) is named "frmZoom" and its control (text box) where the information is to be entered is called "txtZoom".
I would appreciate any help you may have.
Thank you
Private Sub Notes2_DblClick(Cancel As Integer)
If Me.AllowEdits = False Then
Messaggi.MessaggioExclamation
Else
Me.Refresh
DoCmd.OpenForm "frmzoom", acNormal, , , , acDialog
End If
End Sub
Private Sub Form_Close()
Forms("frmMasterListOfEventsDetails")!Notes2 = Me.txtZoom
Forms("frmMasterListOfEventsDetails").Refresh
End Sub
Private Sub Form_Open(Cancel As Integer)
Me.txtZoom = Forms("frmMasterListOfEventsDetails")!Notes2
End Sub
I believe the problem is linked to the fact that the form which I am trying to create the zoom window is actually a sub form embedded in a form
I believe you are correct. Since frmMasterListOfEventsDetails is a subform,
Forms("frmMasterListOfEventsDetails")
will not find it. You need to go through the main form:
Forms("parentFormName").Form.frmMasterListOfEventsDetails.Form.Notes2 = Me.txtZoom
I'm late but you can just use the SHIFT+F2 to get a zoom a any textbox in Microsoft Access
Probably, a better aproach (since you can reuse it on other forms) is this:
Private Sub Notes2_DblClick(Cancel As Integer)
If Me.AllowEdits = False Then
Messaggi.MessaggioExclamation
Else
Me.Refresh
DoCmd.OpenForm "frmzoom", acNormal, , , , acDialog, Me!Notes2
if IsOpened("frmzoom") then
Me!Notes2 = Forms!frmzoom!txtZoom
DoCmd.Close acForm, "frmzoom"
end if
End If
End Sub
'Independent module
Public Function IsOpened (stNameOfForm as string) As Boolean
Dim i As Integer
For i = 0 To Forms.count - 1
If Forms(i).Name = stNameOfForm Then
IsOpened = True
Exit Function
End If
Next
End Function
'frmzoom module
Private Sub Form_Open(Cancel As Integer)
Me.txtZoom = Me.OpenArgs
End Sub
Private Sub Btn_OK_Click() 'frmzoom Button OK
Me.Visible = False
End Sub
Private Sub Btn_Cancel_Click() 'frmzoom Button Cancel
DoCmd.Close
End Sub
As you can see, the zoom dialog receives the data from the form that calls it and then collects the information directly depending on the status (open / closed); thus the Zoom dialog does not need to know the name of any control and can be used by any form.

Form transition and data refresh

I have a form with a button and when I push it, it opens another form. When I close this second form I need to refresh the first form.
So I need to insert the first form name in a global variable to access the form name from the second form to refresh the first form.
I tried create this:
global formname as form
formname = activeform.name
but I receive an error saying: The option is read only.
On the Click event of the button you can pass the name of the form as the OpenArgs property of DoCmd.OpenForm.
Private Sub Command4_Click()
DoCmd.OpenForm "frmStaff", , , , , , Me.Name
End Sub
On the Close event of the second form you can check this (string) value:
Private Sub Form_Close()
If Me.OpenArgs <> "" Then
'MsgBox Me.OpenArgs
Forms(Me.OpenArgs).Refresh 'or .Requery
End If
End Sub
Why don't you just go into the VBA code behind and refresh it with the close sub? I don't understand the need for a global variable to be passed between the forms - You should be able to just do something like this in your second form:
Private Sub Form_Close()
Form_YourFormHere.Refresh
End Sub
If you must use this approach, try this in the first form's module:
Public formname as Form
Private Sub Command1_Click()
Set formname = Me
End Sub
or
Public formname as String
Private Sub Command1_Click()
formname = Me.name
End Sub
A form is an object type, and can only be assigned to a variable using a Set statement, otherwise VBA will think that you are trying to assign the value of the object to the variable.
You said that you wanted the form name, however your variable was a Form type, not a String. The first code snippet will achieve the result you appeared to be trying to achieve, and the second will achieve the result you said you wanted to achieve.
A different approach would probably be better and more reliable, though.