I need to pass 8 database column dates to my CalculateMostRecentDate function since vb6 doesnt have a max function. Am i simply able to pass the database values like this? or how would I do it?
Public Function CalculateMostRecentDate(ParamArray dates() As Variant) As Variant
Dim i As Integer
Dim MostRecentDate As Variant
MostRecentDate = dates(LBound(dates))
For i = LBound(dates) + 1 To UBound(dates)
If MostRecentDate < dates(i) Then MostRecentDate = dates(i)
Next i
CalculateMostRecentDate = MostRecentDate
End Function
Call function within another function:
RECENT_APPRV_DT = CalculateMostRecentDate(EMPLOYER.UW_APPRV_DT, EMPLOYER.BE_APPRV_DT, . . .)
Looks about right.
Google turned up this which looks quite polished, also this from the old VB6 newsgroup.
The function looks OK but you cannot call it the way you have indicated. You will need to load the dates into an array and then pass the array in.
Related
My database has a job number field that consists of year+month/serialnumber+type. There can be multiple jobs with the same job number:
201812/6Door
201812/6Stair
201812/6Wardrobe
When the user wants to change the date of any/all of these records I want all 201812/6 jobs to show in a form.
I have successfully used the OnlyDigits function below to pull only numbers from the text field: OnlyDigits(JobNumber) = 2018126. But I can't figure out how to filter the form to show all jobs containing 2018126.
I have tried using this query but get an error saying expression typed incorrectly or is too complex.
SELECT onlydigits(jobnumber) AS JobNumberDigits, tbldelivery.DelDateDoors, tbldelivery.Lag, tbldelivery.ProductionDate, tbldelivery.OrderNumber, tbldelivery.JobNumber
FROM tbldelivery
WHERE (((onlydigits(jobnumber))=OnlyDigits([Forms]![tblDelivery]![JobNumber])));
I also tried using a where expression in Docmd.OpenForm but that didn't work either. Can anyone suggest how I use this function to filter?
Public Function OnlyDigits(ByVal pInput As String) As String
Static objRegExp As Object
If objRegExp Is Nothing Then
Set objRegExp = CreateObject("VBScript.RegExp")
With objRegExp
.Global = True
.Pattern = "[^\d]"
End With
End If
OnlyDigits = objRegExp.Replace(pInput, vbNullString)
End Function
Try specifying the parameter to free Access from guessing:
PARAMETERS
[Forms]![tblDelivery]![JobNumber] Text;
SELECT
OnlyDigits(JobNumber) AS JobNumberDigits,
tbldelivery.DelDateDoors,
tbldelivery.Lag,
tbldelivery.ProductionDate,
tbldelivery.OrderNumber,
tbldelivery.JobNumber
FROM
tbldelivery
WHERE
OnlyDigits(JobNumber)=OnlyDigits([Forms]![tblDelivery]![JobNumber]);
or:
WHERE
OnlyDigits(CStr(Nz(JobNumber, 0)))=OnlyDigits([Forms]![tblDelivery]![JobNumber]);
I need to convert the values from combo box to a string so I can add that string to a variable, to a function to eventually add to a database.
Here is my sub that grabs text from my form and from the combo box:
Private Sub cbRowStudentGrade_Change()
Course_ID.SetFocus
rowCourseID = Course_ID.Text
StuRed_ID.SetFocus
rowStudentRedID = StuRed_ID.Text
cbRowStudentGrade.SetFocus
cbRowStudentGrade = cbRowStudentGrade.Column(0)
CurrentDb.Execute "qryInputGrades"
MsgBox (rowCourseID)
MsgBox (rowStudentRedID)
MsgBox (cbRowStudentGrade)
Requery
Repaint
End Sub
And here are the functions that I am using as criteria in my Access query builder.
Public Function funcRowCourseID() As String
funcRowCourseID = rowCourseID
End Function
Public Function funcRowStudentRedID() As String
funcRowStudentRedID = rowStudentRedID
End Function
Public Function funcCbRowStudentGrade() As String
funcCbRowStudentGrade = cbRowStudentGrade
End Function
My query:
INSERT INTO tblRegistrationGrade ( Red_ID, Course_ID, Grade )
VALUES (funcRowStudentRedID(), funcRowCourseID(), funcCbRowStudentGrade());
I think there is a datatype mismatch between the database and what the combobox value actually is. But, if there were a datatype mismatch wouldn't there be an error stating as such? My database requires short text, which these are.
You're doing this way too complicated.
You can directly refer from your INSERT query to the form controls:
INSERT INTO tblRegistrationGrade ( Red_ID, Course_ID, Grade )
VALUES (Forms!myForm!StuRed_ID, Forms!myForm!Course_ID, Forms!myForm!cbRowStudentGrade);
and get rid of almost all of the code, except calling the query.
As John wrote, it would be better placed in cbRowStudentGrade_AfterUpdate().
Or, instead of the INSERT query, use a DAO.Recordset with AddNew, see:
How to: Add a Record to a DAO Recordset
There you can also directly use the values from your from controls without worrying about data types.
After applying the unpivot procedure, I have an Amount column that has blanks and other characters ( like "-"). I would like to convert those non-numberic values to zero. I use replace procedure but it only converts one at the time.
Also, I tried to use the following script
/**
Public Overrides Sub Input()_ProcessInputRows(ByVal Row As Input()Buffer)
If Row.ColumnName_IsNull = False Or Row.ColumnName = "" Then
Dim pattern As String = String.Empty
Dim r As Regex = Nothing
pattern = "[^0-9]"
r = New Regex(pattern, RegexOptions.Compiled)
Row.ColumnName = Regex.Replace(Row.ColumnName, pattern, "")
End If
End Sub
**/
but i'm getting error.I don't much about script so maybe I placed in the wrong place. The bottom line is that I need to convert those non-numberic values.
Thank you in advance for your help.
I generally look at regular expressions as a great way to introduce another problem into an existing one.
What I did to simulate your problem was to write a select statement that added 5 rows. 2 with valid numbers, the rest were an empty string, string with spaces and one with a hyphen.
I then wired it up to a Script Component and set the column as read/write
The script I used is as follows. I verified there was a value there and if so, I attempted to convert the value to an integer. If that failed, then I assigned it zero. VB is not my strong suit so if this could have been done more elegantly, please edit my script.
Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)
' Ensure we have data to work with
If Not Row.ColumnName_IsNull Then
' Test whether it's a number or not
' TryCast doesn't work with value types so I'm going the lazy route
Try
' Cast to an integer and then back to string because
' my vb is weak
Row.ColumnName = CStr(CType(Row.ColumnName, Integer))
Catch ex As Exception
Row.ColumnName = 0
End Try
End If
End Sub
Is there a way to call a function, where the call is stored in a table
**Record 1 task Function call**
124567 Email customer Call function emailcus(a,b,c,d)
434535 AddCost Call function addcost(a,b,c,d)
Cheers
Graham
Yes, you can use the Eval() function for that.
Syntax:
Dim ReturnValue As String
ReturnValue = Eval("MyFunction(1, 2)")
Note that you have to provide the exact function call including parameters.
I'm pointing this out because I'm not sure if the parameters a, b, c, d in your example are only dummy values for your example, or if you expect VBA to fill in the values of some variables named a, b, c, d automatically.
The Eval function does not do this, so if you need variable values as parameters, you would have to do something like this:
Dim ReturnValue As String
Dim EvalString As String
EvalString = "MyFunction(" & Variable1 & ", " & Variable2 & ")"
ReturnValue = Eval(EvalString )
This is a variation on the answer already given by haarrrgh, so if you find it useful be sure to upvote that one as well.
There's another way to deal with placeholders in your DB-stored function calls. First, change your data thusly:
**Record 1 task Function call**
124567 Email customer Call function emailcus([TokenA],[TokenB])
434535 AddCost Call function addcost([TokenA],[TokenB])
Note that the [SquareBrackets] are not actually required syntax in this example, just something that I tend to use in this situation. The important part is to make the parameter tokens something that doesn't appear elsewhere in the string value (including other tokens). You can use as many parameters as you need, just make sure that the calling code knows about how many are expected by each function-call string (I cut it down to shorten my following code).
Then when it's time to call your function, do this:
Dim ReturnValue As String 'or as appropriate for individual the function's return
Dim EvalString As String
EvalString = 'code to fetch from table
EvalString = Replace(EvalString, "[TokenA]", strValueA) 'strValueA passed in?
EvalString = Replace(EvalString, "[TokenB]", strValueB) 'strValueB passed in?
ReturnValue = Eval(EvalString)
In VB6, at least (so I assume it's true in VBA), Replace is faster than concatenation. I also find this more readable, but that may be because I'm used to it from using a similar technique to build SQL commands in code (using Const declarations rather than DB storage, but that would work too).
EDIT
As I reread my "finished" post just after submitting it, I realized that there's a gotcha lurking in there. Because you're doing substitution before submitting the string to Eval, these are actual values that are being put into the string, not variables. The code I presented above works fine if your parameters are numeric, but if they're String type you have to include the quotes, either in your data or in your Replace call. I prefer the former, so change your data to this:
**Record 1 task Function call**
124567 Email customer Call function emailcus('[TokenA]','[TokenB]')
434535 AddCost Call function addcost('[TokenA]','[TokenB]')
This works as tested with a Const. Stored in a DB record, you might need this instead:
**Record 1 task Function call**
124567 Email customer Call function emailcus(""[TokenA]"",""[TokenB]"")
434535 AddCost Call function addcost(""[TokenA]"",""[TokenB]"")
(which also works with a Const...).
The alternative is to leave the data as it is in my first part, & change the Replace calls:
EvalString = Replace(EvalString, "[TokenA]", """" & strValueA & """") 'strValueA passed in?
'or maybe
EvalString = Replace(EvalString, "[TokenB]", "'" & strValueB & "'") 'strValueA passed in?
A couple of other potential gotchas: These must be Functions, not Subs, and they must be declared Public in a module, not in a Form's code.
I am using rdlc report, i have a column in database which i want to display in the report.
vehicleDamageArea=1,2,3
In the report I need to mark the placeholders with these values.
=iif((Fields!vehicleDamageArea.Value="3"),Chr(253),Chr(168)) like this.
But as we know,it will check the whole value 1,2,3="3" not the splitted values.
Any suggestion to check by splitting the vehicleDamageArea parameter.
I made it to work as below
Public Shared Function CheckValue(ByVal InString As String,ByVal input as String) As Char
Dim output As String = String.Empty
Dim Parts() As String = InString.ToString().Split(",")
For i As Integer = 0 To Parts.Length - 1
If Parts(i) = input Then
output = Chr(0120)
Exit For
Else
output = Chr(0111)
End If
Next i
Return output
End Function
You can get the individual values using the split function in reporting services. It returns a zero-based string array, so for your example you need
=Split(First(Fields!ID.Value),",")(2)
You should make a function that accept a comma separated expression, than process this string and return a Boolean, then call this function as for boolean value.