VBA DSum not working in access - ms-access

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

Related

Update an attribute in a record

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.

MS Access masquerade form variable value

The Issue
I have a pre-existing Report that I need to run that refers to the code
Forms!frm_report1_selection!ddlState
I am trying to access the report from the form frm_report3 and as such the Report will not be able to find the record stated above.
I can rewrite the report, but, it has roughly 30 subreports that would all need to be copied and updated to find Forms!frm_report3_selection!ddlState
The Question
Is it possible to create an alias (or masquerading) variable to set the value of forms!frm_report1_selection!ddlState when posting from frm_report3_selection?
Set up a variable in your report and set it to the value of the form that is loaded when you open your report.
Use a function like this to see which form is open;
Function IsFormLoaded(strForm As String) As Boolean
Dim frm As Form
Dim bln As Boolean
For Each frm In Forms
If frm.Name = strForm Then
IsFormLoaded = True
End If
Next
End Function
then set your variable depending on which is open when the report loads
Dim ddlState as String
If IsFormLoaded("frm_report1_selection") then
ddlState = Forms!frm_report1_selection!ddlState
ElseIf IsFormLoaded("frm_report3_selection") then
ddlState = Forms!frm_report3_selection!ddlState
End if
Then use the variable in the report.

MS Access: Object Required error

This code is just meant to figure out a total and then a percentage of that total, and then display the percentage amount in a textbox. This code runs when a button on a form is clicked, but it uses data based in a subform on that form, and the textbox that it should display to is on the form. Both the form and the subform are invoked in the class list, so it confuses me as to why it comes up with an "Object Required" error.
Private Sub cmdTest_Click()
'Initialisation
Dim TotalAmount As Integer
Dim GiftAid As Integer
'Processing
If AllForms!frmGiftAid!subfrmqryGiftAid!PaymentIncrement Is Not Null Then
If AllForms!frmGiftAid!subfrmqryGiftAid!PaymentIncrement = "Monthly" Then
TotalAmount = _
(AllForms!frmGiftAid!subfrmqryGiftAid!PaymentAmountPerIncrement * 12)
Else
TotalAmount = _
AllForms!frmGiftAid!subfrmqryGiftAid!PaymentAmountPerIncrement
End If
End If
'Termination
GiftAid = (TotalAmount * 0.25)
AllForms!frmGiftAid!subfrmqryGiftAid!subfrmqryGiftAidtxtGiftAid = GiftAid
End Sub
The line that the error occurs on is the first one that references the subform, so it's:
If AllForms!frmGiftAid!subfrmqryGiftAid!PaymentIncrement Is Not Null Then
I've tried using different variations of the code to reference the subform (just referencing the subform, using "Forms" instead of "AllForms") as well as trying it in the subform itself rather than the form and doing so on different events, such as On_Current() or On_Load(), but nothing has worked for me so far.
Any help that you could offer me would be greatly appreciated.
Reference the subform starting either with Me from the code module of the form, or Form, followed by the name of the form. You then have the name of the subform control, Form, to refer to the object contained, and finally and property of the contained form.
Forms!frmGiftAid!subfrmqryGiftAid.Form!PaymentIncrement
Or
Me.subfrmqryGiftAid.Form.PaymentIncrement
Note that it is important to use the name of the subform control, not the name of the form contained.
This code would run on the parent form and a record would need to be selected on the subform.
If Not IsNull(Me.subfrmqryGiftAid.Form.PaymentIncrement) Then
If Me.subfrmqryGiftAid.Form.PaymentIncrement = "Monthly" Then
TotalAmount = _
Me.subfrmqryGiftAid.Form.PaymentAmountPerIncrement * 12)
Else
TotalAmount = _
Me.subfrmqryGiftAid.Form.PaymentAmountPerIncrement
End If
End If

Referencing global variable - MS Access

Im trying to pass the name of a global variable to a sub routine and would like to know how to reference it. For example I could do the below with a control:
Private Sub passCtrlName(ctlName as String)
Me.Controls(ctlName) = "Whatever"
End Sub
Edit:
For Example
Public imGlobVar As String
Public Sub passGlobVar(frm as Form, ctlName as String, globVar as String)
frm.Controls(ctlName) = globVar
End sub
And call it as
Private Sub imaButton_Click()
imGlobVar = "something"
Call passGlobVar(Me , txtBox1, imGlobVar)
End sub
2nd Edit:
It seems that I could most definitely be barking up the wrong tree here, so I will explain what I'm trying to achieve.
I have a form that has textboxes for the users (risk) address, with a checkbox at the top that lets the user select that this address is the same as the 'contact' details already on the system, and the textboxes are locked.
Populating the textboxes is fine and works. What I use the global variables for is to improve usability (albeit slightly).
The user can add new details, and if they hit the checkbox 'make same as contact' the details that they have entered are stored in the global variables, one for each control.
If the user has made a mistake by hitting the checkbox, they haven't lost these value, and by unchecking the box the entered values are returned.
I hoped to create a sub routine where I could pass the name of the global variable and control and calling this routine, as opposed to writing it out for each control.
I have a feeling that I could be using the wrong technique to achieve my goals. But in answer to my original question, it appears that you can not pass global variables to sub routines in the manner that I wished.
You do not need to pass global variables, you can simply refer to them by name. Note that global variables are reset if an unhandled error occurs.
In http://msdn.microsoft.com/en-us/library/dd897495(office.12).aspx you will find a section on Scope and Lifetime of Variables and Constants.
In a module:
Option Explicit
Public glbVarName As String
Const MyConstant=123
Sub InitVar
glbVarName="Something"
End Sub
Any other module, includeing a form's class module:
Sub SomeSub
MsgBox glbVarName
SomeVar=2+MyConstant
End Sub
If you're asking if you can dynamically reference global variables using a string containing the variable name the answer is no. You could use a single global array and pass the index, which would allow you to dynamically reference an element of the array.
[Edit]
In response to the clarification in the question: You could just save the value of each control to its Tag property when the user checks the checkbox. Then, if the user unchecks the checkbox, you can just loop over your controls and assign the value from the Tag back to the Value of the control.
You could store the values from your controls in a Dictionary object, using the control names as the dictionary keys. Then you can retrieve the value for each control based on the control's name.
Option Compare Database
Option Explicit
Const cstrMyControls As String = "Text0,Text2,Text4,Text6"
Dim objDict As Object
Private Sub chkToggle_Click()
If Me.chkToggle = True Then
Call SaveValues
Else
Call RestoreValues
End If
End Sub
Private Sub SaveValues()
Dim varControls As Variant
Dim i As Long
Set objDict = Nothing 'discard previous saved values '
Set objDict = CreateObject("Scripting.Dictionary")
varControls = Split(cstrMyControls, ",")
For i = 0 To UBound(varControls)
objDict.Add varControls(i), Me.Controls(varControls(i)).Value
Next i
End Sub
Private Sub RestoreValues()
Dim varControls As Variant
Dim i As Long
If objDict Is Nothing Then
'MsgBox "No values to restore." '
Else
varControls = objDict.keys()
For i = 0 To UBound(varControls)
Me.Controls(varControls(i)).Value = objDict(varControls(i))
Next i
End If
End Sub
I use additional field in table - name cancel - of course boolean - when i'm not sure if contents of fields will be valid I set it true. If this field will be true by the end - then I clean up (it may be all record or some fileds of course). Very easy.

Access VBA sub with form as parameter doesn't alter the form

I have a Microsoft Access 2003 file with various tables of data. Each table also has a duplicate of it, with the name '[original table name]_working'. Depending on the user's choices in the switchboard, the form the user choose to view must switch its recordsource table to the working table. I refactored the relevant code to do such a thing into the following function today:
Public Sub SetFormToWorking(ByRef frm As Form)
With frm
.RecordSource = rst![Argument] & "_working"
.Requery
Dim itm As Variant
For Each itm In .Controls
If TypeOf itm Is subForm Then
With Item
Dim childFields As Variant, masterFields As Variant
childFields = .LinkChildFields
masterFields = .LinkMasterFields
.Form.RecordSource = .Form.RecordSource & "_working"
.LinkChildFields = childFields
.LinkMasterFields = masterFields
.Form.Requery
End With
End If
Next
End With
End Sub
The lines of code that call the function look like this:
SetFormToWorking Forms(rst![Argument])
and
SetFormToWorking Forms(cmbTblList)
For some reason, the above function doesn't change the recordsource for the form. I added the 'ByRef' keyword to the parameter just to be certain that it was passing by reference, but no dice. Hopefully someone here can tell me what I've done wrong?
Try removing the parenthesis from your function calls.
SetFormToWorking Forms(rst![Argument])
SetFormToWorking Forms(cmbTblList)
more information
I found the problem. The variable on the third line
rst![Argument]
doesn't exist in the function's scope. I replaced it with
.RecordSource
Unfortunately I'm having another problem with the code but it's unrelated to this question.