Access VBA: set ComboBox height through function - ms-access

I want to use a function to adjust the height of a ComboBox. This is the simplified code :
Private Sub Form_ComboBox_AfterUpdate()
Adjust_Box (Me.Data_Subject_Categories)
End Sub
Private Function Adjust_Box(ctl)
ctl.Height = 300
End Function
But I get the error:
Run-time error '424': Object required
on this row :
ctl.Height = 300
How can I fix it? thanks?

You are using parentheses where you shouldn't, causing evaluation of the parameter - it passes the value of Me.Data_Subject_Categories, not the object.
Use
Call Adjust_Box(Me.Data_Subject_Categories)
or
Adjust_Box Me.Data_Subject_Categories

Related

Error 424 when trying to get a value form combobox in ON Activate event

I have been trying use the value in the combo box however the combobox will be locked if the revision num > 0
it gives me an error when i put it in the ON ACTIVATE event
i got the error 424 on Me.txtMin_DailyDose.Value = Me.cboActive_Name.Column(1).Value
Private Sub Form_Activate()
Me.Requery
' prevents user from changing the client, chemical, active name once the
revision number has become 1 or more
If Me.txtRev.Value > 0 Then
Me.cboClient_Name.Locked = True
Me.cboChemical_Name.Locked = True
Me.cboActive_Name.Locked = True
Me.cboActive_Name.Requery
Me.txtMin_DailyDose.Value = Me.cboActive_Name.Column(1).Value
Me.txtADE_PDE.Value = Me.cboActive_Name.Column(2).Value
Me.txtTTC.Value = Me.cboActive_Name.Column(3).Value
End If
End Sub
Is there anyway i can solve this problem?
When you requery the combobox, it looses its value (if it is unbound as it seems), and then neither column(1) has a value.
So after the requery, set the combobox to some value - or rethink your concept.
Edit:
Property Column(n) carries no (sub)properties, thus:
Me.txtMin_DailyDose.Value = Me.cboActive_Name.Column(1)
Me.txtADE_PDE.Value = Me.cboActive_Name.Column(2)
Me.txtTTC.Value = Me.cboActive_Name.Column(3)

Assiging a 'User Stamp' when saving a record

So the first thing is that every table has 4 fields. Created_By, Created_Date, Last_Updated_By and Last_Updated_Date. This will allow me to keep a track of who saved what records and when. I want a public sub (or function?) to update these fields when any record is saved.
Ideally I don't want to have to copy/call my code from every form, but I will if that's what is necessary. I imagine there is a way of doing this globally.
I currently have the following:
Public gLoggedIn As Integer
Public Function get_global(Global_name As String) As Variant
Select Case Global_name
Case "Employee_ID"
get_global = gLoggedIn
End Select
End Function
My next thing is to add vba code to fill the 2 of the 4 fields. The following is something I have in one of my forms, but like I say, I want to able to achieve this globally on every save.
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.NewRecord = True Then
Me.[Created_By] = gLoggedIn
Me.[Created_Date] = Now()
Else
Me.[Last_Updated_By] = gLoggedIn
Me.[Last_Updated_Date] = Now()
End If
End Sub
How can I do this globally?
I'm not sure if 'me' is the correct reference to us if this is global?
Exactly for your demand, you could use a VBA modul with following sub
Public Sub setTimestamp(objForm As Form)
If objForm.NewRecord = True Then
objForm![Created_By] = gLoggedIn
objForm![Created_Date] = DateTime.Now
Else
objForm![Last_Updated_By] = gLoggedIn
objForm![Last_Updated_Date] = DateTime.Now
End If
End sub
You need to call this method in every form when the Timestamp Update should be fired (BTW: I would recommend an event when a save button is clicked or field update, because an edit in the dataset of the form at form update event will probably cause another form update event and could lead to an endless loop).
call setTimestamp(Me)
But you should alternatively consider using a macro like #E Mett said in comment.
Another hint: If you set the variable gLoggedIn to public, you can refer directly without using a function. (You did this even in your example) A function makes sense when you want to hide the variable inside a modul as private. This is called encapsulation, if you want to learn more about it.
EDIT
Changed code by andre451 ' s suggestion.

MS Access runtime error 2115

In Ms Access, I have two unbound combo-boxes: StateBox and DVPCBox. StateBox is simply a list of U.S. states and DVPCBox contains Employee Names from a query based on the value of StateBox.
I'm trying to set the value of DVPCBox equal to the first item in its list. Since the list of Employees is based on the value of StateBox, I need the value of DVPCBox to update every time StateBox changes. I tried the following:
Private Sub StateBox_AfterUpdate()
Me.DVPCBox.Requery
If (Me.DVPCBox.ListCount = 1) Then
Me.DVPCBox.SetFocus
Me.DVPCBox.ListIndex = 0 //<-Error here
End If
End Sub
But I got runtime error 2115 - The macro or function set to the BeforeUpdate or ValidationRule property for this field is preventing Microsoft Office Access from saving the data in the field.
The strangest thing to me is that I'm not even using the BeforeUpdate Event or ValidationRule (as far as I'm aware.)
ItemData(0) is the first combo box value. So set the combo equal to that.
Private Sub StateBox_AfterUpdate()
Me.DVPCBox.Requery
If (Me.DVPCBox.ListCount >= 1) Then
Me.DVPCBox.SetFocus
'Me.DVPCBox.ListIndex = 0 //<-Error here
Me.DVPCBox = Me.DVPCBox.ItemData(0)
End If
End Sub
I also changed ListCount >= 1 because I assumed you wanted to do the same thing when the combo includes 2 or more rows.

calculation in VBA

have an access database with a form that has multiple textboxes for production data. I need to do a calculation with a few of the boxes, they are set up as
txtA * txtB * txtC = txtD
I need to take the values from each of the boxes and perform this calculation behind the scenes. So I need the value from txtA * txtB * txtC and display the answer to that calculation in txtD. I keep running into issues because of the number of textboxes on my form it will always pick up the wrong data?? HeLP!
Private Sub btnCalculate_Click()
Dim ctrl As Control
Dim txt As TextBox
For Each ctrl In Form.Controls
If TypeOf ctrl Is TextBox Then
Set txt = ctrl
If txt.Name = "txtD" Then
Set txt = ctrl
ctrl.SetFocus
ctrl.Text = calculate
End If
End If
Next ctrl
End Sub
Public Function calculate()
Dim calc1 As Double
calc1 = txtA.Value * txtB.Value * txtC.Value / 144
End Function
I keep getting this error:
Run-time error '2185':
You can't reference a property or mathod for a control unless the control has the focus.
This is in regards to txtA, txtB, txtC.
Try
txtD = calculate()
Or
Me!txtD = calculate()
If, for some reason, you want to access a control by its name, do it like this
Dim name As String
name = "txtD"
Me(name) = calculate()
Your calculation function must assign the result to the function name. A potential problem is that you are ignoring types. Of which type is the result of the function? It will be typed as Variant if you don't specify a type (and a variant can contain about anything). Better
Public Function calculate() As Double
calculate = CDbl(txtA.Value) * CDbl(txtB.Value) * CDbl(txtC.Value) / 144
End Function
Now, everyone who looks at the function knows what kind of data the textboxes should contain and, more important, what kind of result the function returns.
First off, the line
ctrl.Text = calculate
should be
ctrl.Text = calculateBoardFeet()
Next, the code
calc1 = txtA.Value * txtB.Value * txtC.Value / 144
should be
calculateBoardFeet = txtA.Value * txtB.Value * txtC.Value / 144

Null Error on Button Click

I have an access database that has a number of calculations in it. One the the issues I am having is I have multiple textboxes and access want me to enter data into all of them before it calculates the formula. I keep getting an error when I leave a textbox blank
Run-time error '94':
Invalid use of Null
How do I set it to ignore all the nulls. Here is my code
Public Function calculate() as double
calculate = cdbl(textbox1.value) * cdbl(textbox2.value) * cdbl(textbox3.value) * cdbl(textbox4.value) / 144
End Function
Private Sub btn1_click()
Dim x as double
x = calculate
textbox5.value = x
End Sub
Any help would be aprecciated. Thanks!
In your circumstance, I'd use the NZ method.
calculate = cdbl(nz(textbox1.value,1)) * cdbl(nz(textbox2.value,1)) * cdbl(nz(textbox3.value,1)) * cdbl(nz(textbox4.value,1)) / 144