I am using a function in MS Access to return a Query field value with a DLookup. The function passes a value to strPersonType from an existing field in the query.
The function works fine when there is a value present in that field, however when there is no value present it returns #Error. In that case I would like it to return a Null or blank value.
I have tried error handling and If statements (like the one below) and I have been unable to fix it thus far.
No matter what I try the field value comes up as #Error.
Function LookUpPersonType2(strPersonType As String)
'Used to look up Person Type2 in the "Person Type Key" table given PERSON_TYPE
'Returns original supplied name if no variation
If IsNull(strPersonType) Or strPersonType = "" Then
LookUpPersonType2 = ""
Else
If IsNull(DLookup("[Person Type2]", "[Person Type Key]", "[PERSON_TYPE] ='" & strPersonType & "'")) Then
LookUpPersonType2 = strPersonType
Else
LookUpPersonType2 = DLookup("[Person Type2]", "[Person Type Key]", "[PERSON_TYPE] ='" & strPersonType & "'")
End If
End If
Debug.Print LookUpPersonType2 & ","
End Function
I realized that I needed to change the passed parameter to a Variant variable type since there is a possibility that the value will be NULL. Originally it was string variable type and strings cannot be NULL. Thanks!
Related
Access 2016 VBA Code -
Lead in:
fullname is the table field name, tag is the property
msgbox fullname.tag
will show the value of tag
Problem:
Using the field name in a variable then trying to use the value of the variable
Example:
x = "fullname"
msgbox x.tag
does not show the contents of tag, error is: 424 object required
even if I Dim x as field or fields or variant I still can not get it to work.
How do I use the value of x, not x ?
Thank you in advance
You can't use a variable to reference a variable.
HOWEVER, you can most certainly use a string variable to referance a control on a form.
So,
dim strMyCtrl as string
strMyCtrl = "LastName"
Now,
msgbox "value of LastName = " & me!LastName
msgbox "Value of LastName = " & me("LastName")
msgbox "Value of LastName = " & me("LastName").Value
or
msgbox "Value of LastName = " & me(strMyCtrl)
msgbox "Value of LastName = " & me(strMyCtrl).Value
And since you can referance the control with a string, then you can also grab the tag value also
eg:
msgbox "Value of LastName control tag value = " & me(strMyCtrl).tag.
So, if you have controls 1 to 5 named:
TextBox1
TextBox2
TextBox3
TextBox4
TextBox5
You can grab the values like this:
dim i as integer
dim strCtrl as string
For i = 1 to 5
strCtrl = "TextBox" & i
msgbox "Value of " & strCtrl & " is = " & me(strCtrl)
Next i
So, no variable against a variable is allowed. However for controls, or even field names in a recordset, you can use a "string" variable with the name of the column, or as per above the name of the control.
I'm trying to write a module in a form that auto-fills an ExpiryDate control based on a user-input IssueDate control and a DaysValid field in a related table. If there is no data in DaysValid, it treats the record as one that never expires, and leaves ExpiryDate blank.
Here's the module as it currently stands:
Private Sub IssueDate_AfterUpdate()
If Not IsNull(IssueDate) Then
Dim DysVld As Integer
DysVld = DLookup("[DaysValid]", "tblInduction", "[ID] = " & Me.Induction)
If IsNull(DysVld) Then
Exit Sub
End If
ExpiryDate.Value = DysVld + IssueDate
End If
End Sub
If there is no value in tblInduction.DaysValid, I get run time error 94 - invalid use of Null. From this I infer that I'm not allowed to set a variable to a Null value... I could put the following if clause earlier, but that seems more cumbersome than should be necessary. What's best practice on dealing with this? An error handler?
Prevent the error.
Only variant variable can hold Null. The DysVld variable is declared as integer. Either declare it as Variant or change code.
DysVld = Nz(DLookup("[DaysValid]", "tblInduction", "[ID] = " & Me.Induction),0)
If DysVld <> 0
I have problems with the wording of the criteria of a DCount function.
On click() I would like to have the number of occurrences of the field "YearMonth" in a tale in accordance with the value entered in an inbound fields called "Txt_entry".
My code ignores the criteria given (and returns 0) given that its wording is wrong but I cannot find out what would be the correct wording.
Private Sub Ctl3_Click()
Dim db As Database
Dim r As Recordset
Dim YearMonth As Field
Dim Txt_entry As String
Set db = CurrentDb()
Set r = db.OpenRecordset("Table")
Set YearMonth = r.Fields("YearMonth")
MsgBox (DCount("YearMonth", "Table", "[YearMonth]=" & Me.Txt_entry))
End Sub
Thanks!
You can always try:
For a string
DCount("YearMonth", "Table", "[YearMonth]= '" & Me.Txt_entry & "'")
For a date:
DCount("YearMonth", "Table", "[YearMonth]= #" & Me.Txt_entry & "#")
Depending how you store it in your database.
You dont need to set those variables for Dcount or Dlookup(assuming all you want is the count)
Variables only need to be set if you have some other criteria you want to enter in the Where clause of the dcount.
As for the me.Text_Entry , try me.Text_entry.value and see if the box has an actual value.
You can test it by throwing it into msgbox(me.txt_entry.value) That may be the cause of not getting a proper count. As the query would end up as
YearMonth = "" without a proper value.
Try this,
DCount("YearMonth", "Table", "[YearMonth]= #" & Format(Me.Txt_entry, "yyyy\/mm\/dd") & "#")
Im trying to create a function in my VBA where if the record they are trying to insert already exists however it returns a type mismatch.
EventCombo is a integer
MedalCombo is string
Private Sub MyCombo_BeforeUpdate(Cancel As Integer)
If Not IsNull(DLookup("RacerID", "Medals", "RaceID = " + EventCombo.Value _
+ " AND Medal = '" + MedalCombo.Value + "'" )) Then
MsgBox "Record Exists"
End If.
End Sub
What this does (or is supposed to do) is make sure no one else has the same medal in the same race.
What am I doing wrong?
With combo boxes in Access, you need to make sure that .value is really what you want. Often the first column is hidden which is .value while what is visible on the drop down box is not .value. When using a combo box to eliminate confusion I use the .columns property.
Also, to make sure the result from the combo box is a number and not text (as you did not use quotes in your example) I used the val() function to convert the combobox data to a number. If it already is a number, this will have no effect. Otherwise, if it is a digit stored as a string, it will convert it to a number. This might not be strictly necessary but it eliminates another possible problem. If the combobox column has a value which is some text which cannot convert to a number it will return 0 which you can test for in your code.
I cleaned up your code a bit with the following
I replaced the + with & like Remou said
changed .value to .columns(0). If the column you are looking for is not the first one, change 0 to the appropriate value
value() function
removed line continuation _. (Personal preference, feel free to ignore)
Private Sub MyCombo_BeforeUpdate(Cancel As Integer)
If Not IsNull(DLookup("RacerID", "Medals", "RaceID = " & Val(EventCombo.Columns(0)) & " AND Medal = '" & MedalCombo.Columns(0) & "'")) Then
MsgBox "Record Exists"
End If
End Sub
I'm trying to write a VBA function that gets a value from an Access table using a SELECT query. Here's the code:
Function getTableValue(uniqueID As Long, tableName As String, _
idField As String, tableField As String) As Variant
Dim db As Database
Dim rs As Recordset
Dim selectSQL As String
selectSQL = "select * from " & tableName & " where " & idField & "=" & uniqueID
Set db = OpenDatabase(dbPath)
Set rs = db.OpenRecordset(selectSQL)
If rs.RecordCount > 0 Then
rs.MoveLast
rs.MoveFirst
getTableValue = rs.Fields(tableField)
End If
End Function
When the field specified by tableField is a Date/Time type and the field is empty, getTableValue() returns an "Invalid use of Null" error. I thought that having the function return a Variant would allow me to return Null values; what should I do to handle these empty dates?
It turns out that the function's Null value was being passed to a Date/Time variable. I've fixed that by using the following:
dim myDate as date
dim myVar as variant
myVar=getTableValue(idNum,"thisTable","idField", "valueField")
if isNull(myVar) then
myDate=0
else
myDate=myVar
endif
While it doesn't check whether or not myVar returns a Date, valueField's values are of type Date/Time so it's ok.
Have a look at This Link
Variant cannot hold the null valu returned from the DB.
You'd have to check for is null and return the vbNull equivalent (don't know if you can use vbNull in VBA).
HTH
Maybe using Nz ?
getTAbleValue = Nz(rsFields(tableField), SomeReplacementDate)
I guess the point is while using the return value from this function. Either you alway return somethin proper or you have to check the return value of this function.
I have to admit I do not know what a null Date may mean. And well do you have an idea what that could be?
I would have changed the function to return a string. Then use nz() to handle the null by making the string zero-length.
getTableValue = nz(rs.Fields(tableField),"")