Change caption of button to field in a table using Access - ms-access

I am trying to make the caption of a button the content of a field from a table.
Everything I am finding online in forums and MS websites only shows using a macro with the OnClick. I need it to display without every clicking. It also needs to dynamically change every time the field from the table changes.
How can I achieve this? Again, I don't want an event such as OnClick, I want it to simply read as the field from the table reads.

You could simply use the Form's OnCurrent event to set the buttons caption.
On Current:
Private Sub Form_Current()
If Nz(Me.FieldName, "") <> "" Then
Me.btnTest.Caption = Me.FieldName
Else
'Handle blank record here
End If
End Sub
This will make it so ever time the selected record changes the button will update. Also just add the code into the specific field's AfterUpdate event so it changes then as well.
After Update:
Private Sub Textbox1_AfterUpdate()
If Nz(Me.FieldName, "") <> "" Then
Me.btnTest.Caption = Me.FieldName
Else
'Handle blank record here
End If
End Sub

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.

Preventing a user from entering anything but integers into a textbox

I am constructing a form in Access and I have several textboxes that require an integer value. Right now you can enter characters instead of number and a debugging window pops up when the submit button is pressed. How can I prevent users from being able to press the submit button when an invalid entry is made in the interger textbox.
Thank you!
First, make the field a Number type in the table design. Then go down to the "Field properties" and make sure "Field size" is set to Integer. This takes care of decimal numbers because it will round them to the nearest integer when the textbox loses focus.
Next, go into the submit button properties and set Enabled = False. This will gray it out and won't allow the user to click it. You may want to add this code to the form's On Current property so that whenever a record is loaded, it will enable or disable the submit button based on the value that is already there. You will need to "AND" in more integer textbox objects to the same if statement if you have more than one integer textbox on your form:
Private Sub Form_Current()
If Not IsNumeric(Me.IntegerTextBox.Value) Then
Me.SubmitButton.Enabled = False
Else
Me.SubmitButton.Enabled = True
End If
End Sub
Finally, add this code to the On Change event for the textbox. This will enable the button whenever an appropriate a numeric value has been entered and will update the button every time a key is pressed.
Private Sub IntegerTextBox_Change()
If Not IsNumeric(Me.IntegerTextBox.Text) Then
Me.SubmitButton.Enabled = False
Else
Me.SubmitButton.Enabled = True
End If
End Sub
Do this for each integer field on your form.
In table Design make the data Type Number (Integer).

MS Access de-select listbox after running macro/query

So I have a form (frmBookingForm) in Access, and a listbox (lstMyBookings) that displays the results of a query.
Below this, I have a button (cmdDeleteBooking) which runs a delete query using the lstMyBookings selection as input. The button then runs a macro that firstly checks whether a record in the listbox is selected, and if one is, runs the delete query and requeries the data, so the deleted record is no longer shown in the listbox. If not, an error message is displayed.
However, if I then click the button again, it again runs the delete query, even though there is apparently nothing selected in the list box.
Essentially, how can I 'clear' the listbox selection?
I'd prefer a solution that can be done in a macro format, as I have little to no understanding of VBA. However, if providing a VBA solution, I'd greatly appreciate a short explanation of it.
Thanks :)
Looks like this website has a nice little function to do it. Essentially, you need to test if it's a multiselect, and then do one of two things. I suppose if you know ahead of time that it is/isn't a multiselect, you wouldn't even need the "if" statement:
If List0.MultiSelect = 0 Then
List0 = Null
Else
For Each varItem In List0.ItemsSelected
List0.Selected(varItem) = False
Next
End If
If the control's MultiSelect property is set to None, this code just sets List0 to Null. If the control's MultiSelect property is set to anything else, the code loops through all items that are currently selected, and sets the Selected property of that item to False. My example assumes your control is called List0.
EDIT
To use this code, use an event instead of a macro. Here's how you do this:
Right click on the button, select properties
In the Property Sheet window, click on the "Event" tab
Click inside of the "On Click" blank area, and click the dropdown arrow, then select "[Event Procedure]"
Click the ellipsis ("...") to go into the code editor.
In the code editor, your should already have your event for the button (let's assume the button is called Command1:
Private Sub Command1_Click()
End Sub
Add your code in between (assuming the listbox is called List0):
Private Sub Command1_Click()
If List0.MultiSelect = 0 Then
List0 = Null
Else
For Each varItem In List0.ItemsSelected
List0.Selected(varItem) = False
Next
End If
End Sub

Subform losing focus when Adding new record

My DB is split into Front and Back. I have three tables that I am working with in this problem; tblMain, tblDetail, tblNote. I know that the number of records is relatively small but I am designing this for long term use. I have a bound form (frmMain) for tblMain that has a subform (subDetail) for tblDetail. SubDetail has a subform (subNote) for tblNote. When I click a button on the main form, frmMain, the subDetail form becomes visible and allows me to add a new record. I copied the button and the VBA to subDetail so I could do the same thing for adding a record to subNote if needed. However, after the record is created, the subNote form disappears and focus is returned to subDetail. If I move to a new record then back, the subNote form becomes visible and shows an empty record except for the PK. I've checked all the properties of both subDetail and subNote. They are the exact same except for the OnCurrent event for subDetail. I have the following for the subDetail OnCurrent Event.
'Hide subform if no records
Private Sub Form_Current()
Me!ctrlmlsID.SetFocus
With Me!subNote.Form
.Visible = (.RecordsetClone.RecordCount > 0)
End With
End Sub
This hides or displays the subform, subNote, depending on if there is any records or not. Any help in figuring out this behavior would be appreciated.
Here is the code for adding a new record to subNote upon button click.
If Me.Dirty Then Me.Dirty = False
Dim AddTransID As String
AddTransID = Me!ctrlID.Value
Me!subNote.Visible = True
Me!subNote.SetFocus
DoCmd.GoToRecord , , acNewRec
Me!subNote.Form!ctrlID = AddTransID
Me!subNote.Form!ctrlType.SetFocus
I've used breakpoints and the adding record works just fine. It's just the subform going invisible again and the focus being changed back to the parent that is the issue because it won't allow those notes to be added.
I get the impression you are fighting Access here.
You normally do not need a button to create a new record using a properly bound subform with AllowAdditions = True. That will happen automatically.
The problem you are running into may stem from the fact that the new records don't really exist yet until saved (as you observe there is no PK value). So the logic to hide subforms isn't going to work. If you insist on staying with this approach you might try
.Visible = (.RecordsetClone.RecordCount > 0 or .NewRecord)
But I think your life will be easier if you remove the buttons and code you have mentioned and let Access do what it is good at doing. Make sure your subform data binding properties are set right.

How to reference a button in Do While within an If Else statement in VBA Access

I'm a newbie to VBA, and will try to make my question clear. I want to add another If Else statement to say when the button OR is clicked, it should to certain stuff else other. So my question is do I have to code anything within Private Sub OR_Click() ... End Sub first then reference it in my new If Else statement?
If .RecordCount > 0 Then
.MoveFirst
Do While Not .EOF
If Filter Is Nothing Then
Set Filter = XFormToFilter.FilterBuilder
Filter.SetPrimaryFilter PrimaryFilter, primaryTable, primaryKey
End If
'This is where I want the button to go
Filter.AddSubFilter "Filter" & .fields("ID"), _
filterString, targetTable, subformDict(targetTable)
.MoveNext
Loop
End If
Thanks a bunch!
You should have created a toggle button in the form designer (not a standard button). Every control has a generic Name which Access will set unless you change it -- something like ToggleButton115. You can then write code within the filter handler like this:
If ToggleButton15 Then
'OR option has been set; change the filter appropriately
Else
'OR option has not been set
End If
NB: This works because the togglebutton's Value property is True or False depending on whether it has been toggled or not. The Value property is the default property -- the property VBA assumes you want to use when you don't specify a property on an object.
See here for more details.