I am having a challenge with Access 2016.
I have created a button to update one attribute with another.
Example:
QTY Value
Quantity Request Value
Remaining Stock Qty Value
I want to use a button to update the QTY value with the remaining stock qty value.
Private Sub Command67_Click()
Set QTY = QTY - QRAmount
Update Form_PartRequest
End Sub
But when I click the button nothing happens. What I'm missing?
If "attribute" means a field of the table the form is bound to, then:
Private Sub Command67_Click()
Me!QTY.Value = Me!QTY.Value - Me!QRAmount.Value
Me.Dirty = False
End Sub
Rename the controls to something meaningful.
Related
I was wondering if it were possible to have a field shown or active if another previous field is selected.
For example, if I have a Status field and then choose Inactive then I would like another field to show for Inactive Date
Thanks
Let say, you have checkbox for status and textbox to capture date on your form. AfterUpdate event gets triggered when control value changes and you can enable/disable other controls here.
Private Sub chkStatus_AfterUpdate()
If Me.chkStatus Then
Me.txtDate.Enabled = True
Else
Me.txtDate.Enabled = False
End If
End Sub
To enable/disable textbox on dropdown change using VBA
Private Sub cmbStatus_Change()
If Nz(Me.cmbStatus, "") = "Active" Then
Me.txtDate.Enabled = True
ElseIf Nz(Me.cmbStatus, "") = "InActive" Then
Me.txtDate.Enabled = False
End If
End Sub
Using Macro builder - without VBA
I have the following code set up to sum the amount field in my qryExpenses query.
Private Sub Command18_Click()
Dim txtExpense As Currency
txtExpense = DSum("Amount", "qryExpenses")
End Sub
For some reason it is not pulling the amount into my form. Am I missing something?
As it stands now, you have declared a variable to hold the total amount, you retrieve the amount with DSum() but you do not assign its value to the textbox control. In addition I believe you have given the variable the same name as the Textbox control.
If that's the case, change the variable name to something meaningful and then assign its value to the textbox control.
Private Sub Command18_Click()
Dim totalExpense As Currency
totalExpense = DSum("Amount", "qryExpenses")
Me.txtExpense.Value = totalExpense
End Sub
I need to create a Form on access 2010 that navigates among records from a table (t_main)
but the problem is that I need a combo to select among param1_old and new and also among param2_old and new.
This combo will have 2 values, the old and the new for each parameter. In the end, after I select which parameters I want to keep for this user, I will click in a button and save this information into a new table (t_saved).
t_main has the following structure:
user; date; name; param1_old; param1_new; param2_old; param2_new
and
t_saved has the following structure:
user; date; name; param1; param2
Any idea on how to do that? Should I work with recordsets? is there a way to avoid it and just force the combo to take values from 2 different fields into a value list?
Thanks a lot for any help!
edit:
I know it is rather complex to understand what I need, I will try to show in a screenshot:
the data in the table are as following:
user; date; name; param1_old; param1_new; param2_old; param2_new
1234568789;"21/07/2014";"John Smith";'Lemon street 125';'Avocado avenue 123'; '...'; '..'
You don't mention what is the primary key in each tables, so I will assume it's the user field and that each record in t_main and t_saved has a unique user.
If not, then substitute that with the actual primary key.
Note that date is a reserved keyword and you cannot name a field date, so I renamed it thedate instead.
I have created a small sample database that works as you describe (at least as I understand it).
I created these tables with some sample data:
I created a form bound to the t_main table:
Note that the dropdown boxes cbParam1 and cbParam2 are not bound to any field, they are left unbound.
The rowsource for the dropdown boxes is a bit of a hack, but it works well.
For instance, for cbParam1.RowSource:
SELECT param1_old
FROM t_main
WHERE user=Forms![FormMain]![User]
UNION ALL
SELECT param1_new
FROM t_main
WHERE user=Forms![FormMain]![User];
This query selects both the old and new fields from the t_main record that has the same user as the one currently displayed. In effect, it shows both old and new parameters for the current record in the combobox.
The code behind the FormMain is mostly used to manage the display.
Here we prevent the user from adding the data to t_saved if one f the dropdown boxes is empty, or if we've already added that record before.
Option Compare Database
Option Explicit
' We use this variable to keep track of whether the
' record was already found in the t_saved table
Private alreadysaved As Boolean
'-----------------------------------------------------------------------------
' Update the UI after we change our selection of parameter
'-----------------------------------------------------------------------------
Private Sub cbParam1_AfterUpdate()
UpdateUI
End Sub
Private Sub cbParam2_AfterUpdate()
UpdateUI
End Sub
'-----------------------------------------------------------------------------
' Enable/Disable the save button.
' The button is only enabled if the user selected both parameters
'-----------------------------------------------------------------------------
Private Sub UpdateUI()
btAddData.Enabled = Not (IsNull(cbParam1) Or IsNull(cbParam2)) _
And Not alreadysaved
End Sub
'-----------------------------------------------------------------------------
' Refresh teh data every time we change record
'-----------------------------------------------------------------------------
Private Sub Form_Current()
' Reset the values of the parameters comboboxes
cbParam1 = Null
cbParam2 = Null
cbParam1.Requery
cbParam2.Requery
' Check if there is already a record for the same user in the t_save table
alreadysaved = DCount("user", "t_saved", "user='" & user & "'") > 0
' Display a warning to tell the user the current record cannot be saved again
lblInfo.Visible = alreadysaved
UpdateUI
End Sub
The important bit of code is actually the one that adds the data to a new record int he t_saved table:
'-----------------------------------------------------------------------------
' The button was clicked.
' Save the current record data to the t_save table
'-----------------------------------------------------------------------------
Private Sub btAddData_Click()
' We create a new new record in t_save and copy our data
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb()
Set rs = db.OpenRecordset("t_saved", dbOpenDynaset, dbFailOnError)
With rs
.AddNew
!user = user
!thedate = thedate
!name = name
!param1 = cbParam1
!param2 = cbParam2
.Update
.Close
End With
Set rs = Nothing
Set db = Nothing
' Force the form to refresh itself
' This will cause the Form's OnCurrent event to be triggered
Me.Requery
End Sub
I have a Main Form with 3 fields, where the rep would enter the Property, Room Type, and the requested Check-in Date - however on the same form i have a subform displaying Property, Room Type and Check-in Dates NOT AVAILABLE. If a rep enters data in those 3 fields and ALL 3 match what is in the NOT AVAILABLE subform - what code (either VBA or On Lost Focus etc) can i use to look up those values in the subform (so that if it matches what's in the subform it will not allow for submission) and also popup an ERROR message that the "Property, Room Type and Check-In Date you selected are not available" ?
(Main Form data entered will go into a table let's say RoomRequestTable and Not Available subform displays data from another table RoomNotAvailableTable)
Below is some sample code utilizing the BeforeUpdate event. I'm assuming you want to trigger this from any of the fields, so I wrote a sample function that returns false if all of the fields match. We access the subform by calling Me.[SubFromName].Form.
Private function validate() As Boolean
Dim sbfrm As Form
Set sbfrm = Me.SubFormName.Form
If Me.FieldName = sbfrm.FieldName And Me.FieldName2 = sbfrm.FieldName2 And Me.FieldName3 = sbfrm.FieldName3 Then
MsgBox "you can't do that", vbExclamation, "Sorry kid"
'return false
validate = false
Else
'return true
validate = true
End If
End Function
Private Sub ControlName1_BeforeUpdate(Cancel As Integer)
Cancel = Not validate
If Cancel then
Me.ControlName1.Undo
End If
End Sub
Private Sub ControlName2_BeforeUpdate(Cancel As Integer)
Cancel = Not validate
If Cancel then
Me.ControlName2.Undo
End If
End Sub
Private Sub ControlName3_BeforeUpdate(Cancel As Integer)
Cancel = Not validate
If Cancel then
Me.ControlName3.Undo
End If
End Sub
I lookud up information about this topic, but without any effect.
I have a Access' table called TbKlient, which contains columns ID (primary key, autonumbered) and Name (Name of the Company) and Form FormVT1 with combobox cbName. My aim is simple but I can't achieve this. So I want, when I open this form, auto populate default value of cbName combobox with first row of TbKlient's Name column.
Appreciate all sugestions. Thank you!
Try to assign onOpen event form form FromVT1
Private Sub Form_Open(Cancel As Integer)
dim rst as recordset
set rst=currentdb().openrecordset("SELECT [Name] FROM [TbKlient] ORDER BY [ID]") ' author, please check if you really want record with the lowest ID that is called by you "first record"
if not rst.eof then
Me.cbName.DefaultValue = rst![Name]
end if
rst.close
set rst=nothing
end sub