Enable / Disable Field based on Combo Selection - ms-access

I am new to Access and need assistance in enabling / disabling a field based on another field's selection.
I have a combo box called RegistrationType and if "Individual" is chosen I would like the "Business_Name" textbox to be disabled and if "Company" is chosen I would like it enabled.
Your help would be greatly appreciated :)
I am still having trouble getting this to work. I've tried everything.
So basically i just want to enable "Business Name", "ABN" & "Business Phone" when "Company" is chosen from "Registration Profile" and if "Individual" is chosen to disable them.
if anyone can help that would be amazing. I appreciate everyone trying.
enter image description here

A simple solution :)
Private Sub Form_Current()
setRegistrationType
End Sub
Private Sub RegistrationType_AfterUpdate()
setRegistrationType
End Sub
Private Sub setRegistrationType()
Me![Business_Name].Enabled = RegistrationType & "" = "Business"
End Sub
This works well for Form-view but not for Continues forms.
Added the empty string in setRegistrationType to handle Null-values in Registration type.

Related

How to pass value from subform to mainform ms access

I have a form with a subform (Datasheet view form). When i click a row in subform, i want pass that row value to text box in main form . In subform even wrote this
Private Sub Form_Click()
MsgBox (Noloan)
End Sub
But i don't know how to pass that subform form_click no_loan to mainform.textbox. Thanks for your help.
Additional data:In my db , i have a table which consist field No,NoLoan,Name. NoLoan Field value as i wanna clicked .From that table i create a subform/datasheet view ,subform name is T_Loan1. Then I create Main Form, name is FindLoan2. In that main/parent form i create text box, called Text7, and put T_Loan1 in subform object at footer.
I put this
=[Forms]![FindLoan2]![subformTLOAN].[Form]![Noloan]
in my Text7 Control and It's work.
Thank's for all attention.
Goto to the subform and click on the field which has the row value
Select Field property - click on events tab --> On Click --> Code Builder
Put below code in the Click event where Form_frmMain is your form name and Me.ID is the field.
Private Sub ID_Click()
Forms!FindLoan2.Text7 = Me.ID
End Sub
The best way is to write code that ignores the forms!names. If you copy the form, or change the sub form, or even drop the sub form into a different form, then all your code breaks.
To reference the textbox in the parent form from code, use:
me.Parent!Text7 = me!id
I am not sure which version of MS Access you are using, but it is also possible to use Tempvars to store values for use elsewhere if you have a complicated application which makes referencing another form/field/control difficult.
To create:
Tempvars.Add "TempVarName", TempvarValue
To use:
Tempvars!TempVarName
To remove:
Tempvars.Remove "TempVarName
I try to limit my use of them, as it becomes hard to manage. I am always careful about when I set the values and I ensure that they are removed after I am finished with them to avoid confusion or issues with values persisting when they shouldn't.

Tab control in VB

I'm working on a legacy VB6 application. I'm sure this probably relates to VB.NET so i will tag it, but please let me know if it's completely different(which I dont think it is) then I'll remove the tag to avoid confusion.
Here is my issue....
I have a Tab control with multiple tabs: 0 - 3. On TabStuff.Tab = 0, I have a few textboxes and comboboxes. The user uses keyboard TAB to move from Indexed controls. What happens is once they get to the last control which is a textbox called txtCity - and click keyboard TAB once more, it brings them to TabStuff.Tab=1.
My issue is I do VALIDATE on txtCity - I call a function that verifies that a couple of the fields aren't NULL and if one of the fields is in fact NULL then I show a MSgBox and try to setFocus on that control. But instead, when OK is clicked on msgbox, it goes to the next tab which is TabStuff.tab=1 which is not correct.
Here's some of my code...
Dim FirstName, City as String
flag=false
firstName = txtName.text
city = txtcity.text
if FirstName="" or isnull(FirstName) then
msgbox "Please enter Name"
tabstuff.tab=0
txtname.setfocus
exit sub
elseif city = "" or isnull(city) then
msgbox "Please enter city"
tabstuff.tab=0
txtcity.setfocus
exit sub
end if
flag=true
This code is in txtCITY_VALIDATE
So in case city was empty, it shows MsgBox, stays on Tab=0 and setfocus on that control, but instead it goes to the next tab=1 and sets focus on the first control of that tab.
EDIT:
in txtCITY_LostFocus
If Flag = False Then
TabStuff.Tab = 0
Exit Sub
End If
I added this but it still goes to tabstuff.tab=1 setting the focus on first control of the tab
EDIT 2:
In a new project i created txt1 and txt2 - i set TabIndex 0 and 1 respectively.
Private Sub Txt1_Validate(Cancel As Boolean)
If Txt1.Text = "" Then
MsgBox "no text in txt1"
Txt1.SetFocus
End If
End Sub
This is the code I use. I click TAB on txt1 without entering any text, so this gets executed, but after msgbox, the focus gets set on txt2
For some extremely weird reason - i seem to have been getting this discrepancy because I was doign it in the VALIDATE property. When i entered the same code in LostFOCUS it seems to work fine. Thanks everyone for your help with this!

Referring to textbox name in click event

I am trying to find a way to automatically refer to a textbox name within its click event. The name will be used in a sub called by the event code. I know that it can be hard coded, but I have many textboxes and would prefer to have general purpose code. Any help would be appreciated.
EDIT:
Yeah, I should have typed 'label' instead of 'textbox' - sorry. Anyway, I'd like to be able to use a simple bit of code like:
Private Sub lbl_Charges_Click()
tabPage (me.activecontrol.name)
End Sub
Obviously, the activecontrol method does not work on labels. The code I'm calling is:
Private Sub tabPage(page As String)
page = Mid(page, 5)
Echo False
Me.tab_Menu.Pages("pag_" & page).SetFocus
Me.tab_Menu.Visible = True
Me.lbl_Hide.SetFocus
Echo True
End Sub
The reason I'm doing all of this is that I'm trying to find a way to get rid of all of the flicker associated with this (tab menu and label backgrounds are transparent) The echo method will take care of the flicker with the tab menu pages, but doesn't help with the command buttons.
TIA
You can use ActiveControl:
Call SomeClickFunction(Me.ActiveControl.Name)
or, to pass the control itself:
Call SomeClickFunction(Me.ActiveControl)

Access 2007 textbox is seen as empty while it is not

I was searching for my question for hours and I have found some ideas but it still doesn't do what I want.
I have a form "AddWorker" with texboxes "Name", "Surname" and so on.
At the end of this form I decided to use a button to handle addition to database by VBA code.
When I put some data into texboxes I can refer to them by textbox.text property, but only if I am focused on this texbox. In other cases I can use textbox.value property.
In my case when I put all data into 3 textboxes I clik button "Add worker" to add person to database, but last textbox (for example 3) is seen as empty because it did't see text I have put into textbox. I need to clik into another textbox (for example 2 or 1) to create "some event" and then it update textbox 3 and all data can be read in vBA.
What can I do to see all texboxes filled in VBA when i click "Add worker" button.
I have found some example but it didn't help me a lot. I still see empty textbox while clicking "Add worker" button.
Textbox null problem
First of all, make sure the form is unbound. It should not have a recordsource associated with it.
Then, try adding a few messageboxes in the code behind the Add Worker button. Something like:
Sub Add_Worker_Click()
msgbox "Text1: " & Me.TextBox1.Value
msgbox "Text2: " & Me.TextBox2.Value
msgbox "Text3: " & Me.TextBox3.Value
/* Comment out all of your code for now */
End Sub
Give that a shot and see what is in the textboxes. Then replace ".Value" with ".Text" and see what you get.

Opposite of combo box .Dropdown method?

Is there a way to toggle the dropdown of a combo box through VBA? .Dropdown is a method and it only works in one direction. I'm looking for the following functionality:
MyCombo.Dropdown = True
MyCombo.Dropdown = False
Obviously the above does not work in MS Access. I'm guessing I'll need to use some sort of hack/workaround for this. I'd like to avoid a .Requery. That would probably work, but there could be a major performance penalty depending on what the source of the combo box is.
I was dealing this this issue today, and after a lot of trial and error, I discovered that the following works beautifully in Access 2010:
With m_cboLookup
.ListWidth = .ListWidth ' Close combo box
'If Len(strSearch) > 3 Then .Dropdown
End With
Essentially, you are just setting the list width to the existing list width, but the combo box is closed, presumably to prepare for redrawing at a different size.
How about setting the focus to another control on the form? That should close the combo box just as if the user had moved the focus somewhere else on the form.
I was just dealing with this as well. The best I could come up with is below. It sends the ALT key twice, which closes the combobox without triggering an Undo or moving focus to another control.
SendKeys "%"
SendKeys "%"
Have you thought about a
SendKeys "{TAB}"
doesn't require you to send focus on any particular control but moves focus off this one
I had tried everything to achieve my desired combo box behavior. I finally found a method that works for me. It's way too elaborate to be the ideal solution, but it works. I tried Adam's listwidth reset method, but it didn't work for me in Access 2013. I had tried the Sendkeys method, but that caused my clients' Num Lock to be turned off. This code gives me perfect combo box behavior.
'The following code goes in a non-class module.
Public booListOpen As Boolean
Public sub subDropDown()
If booListOpen = False Then
Screen.ActiveControl.Dropdown
booListOpen = True
End If
End Sub
'The following code goes in the form module.
Private Sub cboList_Enter()
booListOpen = False
End Sub
Private Sub cboList_Change()
subDropDown
End Sub
Private Sub cboList_Click()
booListOpen = True
End Sub
I know this is an old thread but I had the same problem and tried several solutions. The .ListWidth didn't work for me. It did close the dropdown, but it displayed the bound column values (the IDs) in the 'text box' part of the combobox after the dropdown was closed. Mine is also a multi-select combobox bound to a multi-value field; may be different that the OP.
I was able to solve it by doing .Requery
worked for me.
Combo on worksheet.
SendKeys "{ESC}"
SendKeys "%{Down}"