Word Form - using VBA to enable textbox on checkbox click - forms

I have the following VBA code to enable a textbox everytime a checkbox is clicked.
Private Sub CheckName1_Click()
If CheckName1.Value = True Then
TextName1.Enabled = False
TextName1.SpecialEffect = fmSpecialEffectFlat
Else
TextName1.Enabled = True
TextName1.SpecialEffect = fmSpecialEffectSunken
End If
End Sub
The problem is my document will probably have 30 of these by the time I am done (CheckName1, CheckName2, CheckRent1, CheckRent2, etc). I am already having trouble with Word lagging, so I'd like to create a subroutine that will call this sub instead of copy/pasting it again and again.
I haven't spent a lot of time with functions and the like- and even less with VBA. I just know the basics and they always make my head spin. So, this is what I have.
I just don't know how to pass the textbox parameter, or if I even have the checkbox parameter right.
And if this is close, do I still create a call for each checkbox?
Private Sub CheckName1_Click()
Call NA
End Sub
Private Sub NA(checkbox, textbox)
If checkbox.value = True Then
textbox.Enabled = False
textbox.SpecialEffect = fmSpecialEffectFlat
Else
textbox.Enabled = True
textbox.SpecialEffect = fmSpecialEffectSunken
End Sub
Thanks in advance for any help!

Loop on all controls
Based on this example: http://www.ozgrid.com/VBA/control-loop.htm
Dim cCont As Control
For Each cCont In Me.Controls
If TypeName(cCont) = "TextBox" Then
'DO STUFF HERE
End If
Next cCont

Related

Outlook form VBScript for controls

I've created an Outlook form with several bound controls. I would like one of the check boxes when checked to make two text areas and their labels visible, then not be visible if the check box is unchecked. The controls that I want to make visible have the initial state of not-visible. I'm stuck on the VBScript syntax. I've been using Sue Mosher's Outlook programming book, but I'm a newby and I'm not getting it. I'd appreciate any help with this. I'm using Outlook 2010 on a Windows 7 machine. Thanks!
Here's my code:
Sub Item_PropertyChange(byVal Name)
Set objInsp = Item.GetInspector
Set objPage = objInsp.ModifiedFormPages("Message")
Set ckWGC = objPage.Controls("ckWGC")
Set lblState = objPage.Controls("lblState")
Set WGCState = objPage.Controls("WGCState")
Set lblCountry = objPage.Controls("lblCountry")
Set WGCCountry = objPage.Controls("WGCCountry")
If ckWGC.Value = True Then
lblState.Visible = True
WGCState.Visible = True
lblCountry.Visible = True
WGCCountry.Visible = True
Else
lblState.Visible = False
WGCState.Visible = False
lblCountry.Visible = False
WGCCountry.Visible = False
End If
End Sub
You would want to handle the CheckBox.Change event to capture when the user has checked or unchecked it.
Private Sub CheckBox1_Change()
End Sub

VBA Access form: Submit Button to create new record but keep some values in the form

I have a form to enter all necessary data. With the wizard I created a button that saves the data as a new record, but this button clears the form.
I want this button to do exactly that, but keep some of the entered values in the form, because those values will stay the same for a certain amount of records.
Currently my button runs this code generated by the button creation wizard:
Private Sub submit_btn_Click()
On Error GoTo submit_btn_Click_Err
On Error Resume Next
DoCmd.GoToRecord , "", acNewRec
If (MacroError <> 0) Then
Beep
MsgBox MacroError.Description, vbOKOnly, ""
End If
submit_btn_Click_Exit:
Exit Sub
submit_btn_Click_Err:
MsgBox Error$
Resume submit_btn_Click_Exit
End Sub
When the button is clicked I want to clear all values in the form, except for the date and a group field. Can I easily do this in this code or is there a way to do this via the default value property of these fields?
Try the following:
Go to the properties of your form and put the following code in the BeforeUpdate event:
Private Sub Form_BeforeUpdate(Cancel As Integer)
entry_date.Tag = CLng(entry_date.Value)
acc_value.Tag = acc_value.Value
End Sub
In the Current event of the form, put the following code:
Private Sub Form_Current()
If Me.NewRecord Then
entry_date = CDate(entry_date.Tag)
acc_value = acc_value.Tag
End If
End Sub
That is all you need to do. If you have any questions please let me know

Can't change Visible property in Label of a VBA UserForm

I've got a Excel form named UserForm1 with a label with this properties:
This label has the property Visible=False. I want to make visible the label when the user click the CommandButton1, for that I wrote the next code:
Private Sub CommandButton1_Click()
Me.Label6.Visible = True
Dim oficina_garsa, file_source, file_solds As String
Dim invoice_year, invoice_month As Integer
oficina_garsa = TextBox3.Value
file_source = TextBox1.Value
file_solds = TextBox2.Value
invoice_year = CInt(ComboBox1.Value)
invoice_month = ComboBox2.ListIndex
Debug.Print oficina_garsa, file_source, file_solds, invoice_year, invoice_month
Call MainProcess(oficina_garsa, file_source, file_solds, invoice_year, invoice_month)
End Sub
But it doesn't work. So I wrote other event code linked to CommandButton2 to testing purpose like this:
Private Sub CommandButton2_Click()
If Me.Label6.Visible = False Then
Me.Label6.Visible = True
Else
Me.Label6.Visible = False
End If
Application.Wait Now + TimeValue("00:00:03")
End Sub
And the last code works fine while the first one doesn't! But in the first code the next code after Me.Label6.Visible = True runs whole without error message.
I've tried replacing 'Me' by 'UserForm1', but the result is the same. Why assigning the Visible property to True works in the CommandButton2 event but doesn't in the CommandButton1 one?
I know this thread is quite old, but as I came across it with the same problem, I thought I'd post the fix.
After setting the visible argument, add a line
Me.Repaint
This will re-render the form and update the visibility of the element. I think the Repaint command is specific to User Forms, so not sure if this will work in other situations where elements are not rendering accurately.

Access VBA visible control form from another form

I have a table and form setup to control another form in my database.
I'm wanting to make a code that will take the title from my field and add it to my code as a variable to change the visibility options of my other form.
my form is set with all the of the names to all objects on the form I want to control.
LSE_FORM_ADMIN = The table with all the LSE_FORM_ALL names in it.
Table is setup with 3 columns key, names and a checkbox which I put into a form to make a continuous list.
here is my code on the form, but I keep getting and runtime 424: object required error:
Private Sub Form_Current()
Dim VARSET As Object
Dim VAR As String
VARSET = DLookup("TITLE", Table!LSE_FORM_ADMIN, "") 'keep getting error here
VAR = VARSET
If Me!CB = "-1" Then
Form_LSE_FORM_ALL!VAR.Visible = True
Else
Form_LSE_FORM_ALL!VAR.Visible = False
End If
End Sub
can someone help me fix this code so that it will grab the title field data and make it a variable to add to the rest of the code?
It's difficult to see exactly what you are trying to achieve, but your problems stem from using the variant variable type when you should be using an explicit Form or Control type. Using your last example.
RSTT.Visible = True 'getting Run-time error '424': object required
This is because you have declared RSTT as a variant. The line
RSTT = "Form_LSE_FORM_ALL" & "!" & (RST)
results in the variable RSTT containing a string, which does not have a property ".Visible"
Set DB = CurrentDb
Set RS = DB.OpenRecordset("LSE_FORM_ADMIN")
These lines are redundant as you have the values that you need available on the form fields which are already bound to the table LSE_FORM_ADMIN.
As far as I understand, you have a continuous form (ADMIN?) bound to the table LSE_FORM_Admin. As you step through the records on this form, you want code to be fired which takes the value of the TITLE field/control and use it to set a control with the same name, on a separate form, Form_LSE_FORM_ALL, to be (in)visible, dependent on the value of the checkbox control name CB on the ADMIN form?
If you want the ADMIN form to make the changes "live" to the ALL form, you should consider using an event of the CB checkbox control. Using the current event of the form means that the changes you make will not be reflected in the ALL form until you step out of the record you have just edited, then back in, to fire the form's Current event on that record.
Example using AfterUpdate event of CB checkbox
Private Sub CB_AfterUpdate()
Dim strRST As String
Dim frmTarget as Form
Dim ctlRSTT As Control
Set strRST = Me!TITLE
Set frmTarget = Forms("Form_LSE_FORM_ALL")
Set ctlRSTT = frmTarget.Controls(strRST)
ctlRSTT.Visible = Me!CB 'getting Run-time error '424': object required
End Sub
really not sure how to do the syntax when doing a recordset to a table from the form, need some help with that.
here is my code and attempt at the record set:
Private Sub Form_Current()
Dim DB As Database
Dim RS As Recordset
Dim RST As String
Set DB = CurrentDb
Set RS = DB.OpenRecordset("LSE_FORM_ADMIN")
Set RST = RS 'GETTING OBJECT REQUIRED ERROR ON "RST ="
Do Until RS.EOF
RST = Me!TITLE
RS.MoveNext
Loop
If Me!CB = "-1" Then
Form_LSE_FORM_ALL!RS.Visible = True
Else
Form_LSE_FORM_ALL!RS.Visible = False
End If
End Sub
I think I know what you are trying to do, but your descriptions / references are not matching up. Please look at the following comments and clarify:
1. You say "...make a code that will take the title from my field and ..." but your code is taking "Me.Title", "ME" is a reference to the Form - not a field.
2. Your code is in the "Form_Current" event, which means it will fire for every record you process. That will work, but I think you want to do this code only once to be more efficient.
3. You have no provision for processing more than one field. I think you need to loop through all fields in your table, setting visible to true or false.
The following is my suggestion, but I will update once you clarify the issues.
Option Compare Database
Option Explicit
Dim DB As DAO.Database
Dim RS As DAO.Recordset
'Dim RST As Variant
'Dim RSTT As Variant
Public Sub FORM_CURRENT()
Set DB = CurrentDb
Set RS = DB.OpenRecordset("LSE_FORM_ADMIN")
Do While Not RS.EOF ' Loop thru all field names for the form
If RS!HideYN = True Then ' Desire to hide the field?
Me(RS!ctlname).Visible = False ' Yes, hide the field.
Else
Me(RS!ctlname).Visible = True ' No, show the field
End If
RS.MoveNext ' Get next field name
Loop
RS.Close
Set RS = Nothing
Set DB = Nothing
'Set RST = Me!Title
'RSTT = "Form_LSE_FORM_ALL" & "!" & (RST)
'If Me!CB = "-1" Then
' RSTT.Visible = True 'getting Run-time error '424': object required
'Else
' RSTT.Visible = False
'End If
End Sub
Final code, thanks to Cheesenbranston.
Private Sub Form_AfterUpdate()
Dim strRST As String
Dim frmTarget As Form
Dim ctlRSTT As Control
strRST = Me!TITLE
Set frmTarget = Forms("LSE_FORM_ALL")
Set ctlRSTT = frmTarget.Controls(strRST)
If Me!CB = "-1" Then
ctlRSTT.Visible = True
Else
ctlRSTT.Visible = False
End If
End Sub
#Cheesenbranston: Your original code was more like a toggle of on and off so if my object was not visible then my trigger checkbox would make it visible when checked, more of a quality of life for my own needs, none the less worked. Also strRST doesn't need SET since its just a String. Thanks again =D very happy day!

form_load subroutine in MS Access VBA

I am writing a code in MS Access VBA, which is as follows:
Private Sub Form_Load()
MsgBox "loggedIn = " + CStr(loggedIn)
If (loggedIn = 1) Then
Else
Exit Sub
End If
End Sub
I want to decide whether to load the form or not based on loggedIn variable. If loggedIn variable is 1, form shall be loaded. If the same is not 1, form shall not be loaded.
Issue that I am facing is that, whatever I do in ELSE part of the code above, I am not able to stop the form from loading.
How can I achieve this?
Pls comment if any additional information is required.
Thank you.
If the loggedIn value is available at form open, you could cancel the form open event.
Private Sub Form_Open(Cancel As Integer)
Cancel = Not (loggedIn = 1)
End Sub
If the value of loggedIn is not available until the form's load event, you can close the form.
Private Sub Form_Load()
If loggedIn <> 1 Then
DoCmd.Close acForm, Me.Name
End If
End Sub