2 commands on single button - ms-access

I have a button, and on the button I’m currently using the “DoCmd.OpenForm” command, but would also like the same button to clear the table contents from the previous user on single click. Is this possible?
The button is a Begin button, and once the member clicks begin it will take them to another form to complete, but I want it to clear table contents on the same click.

You can modify the code that the Access Wizard has created to have as many commands as you want. In this case, the code should look like:
Private Sub Command4_Click()
CurrentDb.Execute "DELETE * FROM [Table1];"
DoCmd.OpenForm "Form13"
End Sub
Regards,

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.

Update result of formula in text box

I'm trying to update an unbound text box in a form I'm building.
The text box has a default value which is: =DLookUp("[Metadata]![Username]";"[Metadata]").
When the value in the field username is changed;
how do I update this change in the text box without closing the whole form and then opening it again?
--
I've tried to to do this by adding a button in the form and then on mouse up event try and either by vba or the macro editor find a solution.
It would supposedly be possible to create a macro that will close and then open the form again but it's not really what I'm looking for.
VBA: I found lots of suggestions for requery but I couldn't get this to work:
Set UserName = Forms![Update test]!Text10
UserName.Requery
A general solution that could work with multiple such text boxes is preferable
(but not required, anything that tries to point me in the right direction is welcome).
Go to the design view and select the textbox which represents your table value username, then go to events and select the On Change() event:
Private Sub username_Change()
YourTextbox.Value =DLookUp("[Metadata]![Username]";"[Metadata]")
End Sub

Change caption of button to field in a table using 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

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

use caption of pressed button from main form in query of other form

My problem is:
Main form has 9 command buttons and continuous subform. Click on some button open other form with text box for entering quantity and two command buttons. When click on the first button I want to use caption of pressed button from main form for query. How to get that button caption?
Thanks.
You have just clicked the button, you know the name of the control, so I do not quite see the problem, other than that it all sounds like quite a bad idea.
Private Sub cmdClickMe_Click()
Debug.Print Me.cmdClickMe.Caption
Debug.Print Screen.ActiveControl.Caption
End Sub