MS Access calling a function - ms-access-2010

I am writing a programme for membership of a club in Access. Cell 1 will contain a number and cell 2 will contain cell 1 number in roman numerals.
I have a function that will convert the number but am having trouble getting cell 1 value into the function and the answer into cell 2. The start of the function is
Public Function RomanNumeral(ByVal aValue As Long) As String
and ends with
RomanNumeral = strResult
I would be very happy if anyone can help

Ok, this would be a calculated field. For this, you use =myFunction() as ControlSource.
In your case, if the number field has the name myNumber, use this for the roman number field:
=RomanNumeral([myNumber])
Edit
If you don't want a calculated field, but a field in the table, create an AfterUpdate event procedure for the number field, where you set the second field:
Private Sub myNumber_AfterUpdate()
' Use Nz to avoid runtime error when myNumber is NULL
Me!RomanNumber = RomanNumeral(Nz(Me!myNumber, 0))
End Sub

Related

Using InStr in Access VBA to lookup a string value from another table

Very new to access VBA and would love some guidance on this.
I am searching through a string and looking for a particular substring in this field. This substring would have a value based on another table, ie
order = "Reference order QQ131415"
The problem is, there is no particular pattern for order numbers. Some are 7 digits, some are 10 and some have dashes in there.
There is a table i have access too that has these order numbers though, and I guess I am trying to use that table as a dictionary.
my very very basic Access VBA code is like this
' order= Instr(1, rst![order], qst![order_id],vbBinaryCompare)'
order is the string where i have the order id i am trying to extract
order_id is the actual id from a seperate table.
Is this something that Access VBA can handle?
So i think this will help you
You're not very clear in the sentence following on from "order=" with your code. Could you clear that up.
Instr(string1, String2, [ compare ])
string1 Required. String expression being searched.
string2 Required. String expression sought.
Instr returns a Variant (number in this case) specifying the position of the first occurrence of one string within another. So it should be:
Position= InStr(1, string1, string2, 1)
So you'd now know that the ordernumber starts at position x in Order. You'd then have to do a left(Order, Len(order) -x) to extract the string.
Youd do that in a nested loop because youll have to loop through your dictionary per record in Order.
E.g.
Dim rsOrder as DAO.recordset
Dim rsOrderId as Dao.recordset
dim orderTest as integer
dim stringcapture as string
Set rsOrder = Currentdb.OpenRecordset("[SELECT STRING]")
rsOrder.Movefirst
Do until rsOrder.EOF or rsOrder.BOF
Order = rsOrder![OrderFieldName]
Set rsOrderId = Currentdb.OpenRecordSet("[SELECT STRING]")
rsOrderID.Movefirst
Do Until rsOrderID.EOF or rsOrderID.BOF
OrderID = rsOrderID![OrderIDFieldName]
orderTest = Instr(1, Order, OrderID,1)
StringCapture = left(Order, Len(order) -OrderTest)
rsOrderID.movenext
Loop
rsOrder.movenext
Loop
rsOrder.close
rsOrderID.close
Something to that affect.

Display grand total as the sum of averages (custom subtotal)

I have SQL Server query, which returns the following data:
I want to display the data in RDLC 2008 Report using matrix format which should give the following result:
The Grand Total of Qty field should return 12 for January & 14 for February.
I have tried many different methods one of which is by using the following expression in the matrix 'Qty' textbox :
=IIF(InScope("RowGroup_Category")
,IIF(InScope("RowGroup_SubCategory")
,Fields!Qty.Value
,Code.GetAverageMemberCount(Cint(Avg(Fields!Qty.Value)))
)
,Code.TotalMemberCount
)
The above functions are written in Report Properties Code as below:
Public Dim TotalMemberCount As Integer = 0
Function GetAverageMemberCount(ByVal AverageMemberCount As Integer) As Integer
TotalMemberCount = TotalMemberCount + AverageMemberCount
Return AverageMemberCount
End Function
I have also tried RunningValue(Fields!Qty.Value,Sum,"RowGroup_Category") and many such functions but I am unable to get the exact results. any help would be appreciated.. Thank you
Try adding this into a new column as a test:
RunningValue(Avg(Fields!MemberCount.Value,"RowGroup_Category"),SUM,Nothing)
If the value is correct you should be able to change SUM into MAX when setting this expression in the grand total field.
You can refer to the total like code.TotalMemberCount instead of using a get function
but i don't think you need this function in this case.
Check the following blog for a simular variable referencing situation
The only solution I could find that worked for me to solve this is to calculate the averages in a different dataset and use lookup functions to fill the grand total from there.
In your case I would add a key column in your original dataset:
select Category + '|' + Month as key, Category, SubCategory, Month, Qty, Amt
from YourTable
Create another dataset using:
select Category + '|' + Month as key, Category, Month, avg(Qty)
from YourTable
group by Category, Month
Add the second result as DataSet2 to the report. (In Visual Studio in the Report Data pane right click on DataSets.)
Add to the Report Properties -> Code section the following:
Function SumArray(varArray as array) as Decimal
Dim retVal As Decimal = 0
For Each item As Decimal In varArray
retVal = retVal + item
Next
Return retVal
End Function
Finally in the report use the following expression for Grand Total under Qty:
=code.SumArray(Lookupset(Fields!key.Value, Fields!key.Value, Fields!qty.Value, "DataSet2"))
P.S.: Make sure that the second dataset is also filled by your code the same way the original was.

How to store the string array into databse?

In my java servlet application!
i am getting the checkbox selected values
as in the form of String[] by using the
String[] skills = request.getParameterValues("skills");
Now i have to store this string[] skillset values into oracle db!
I got 2 small solutions
1) i will take that string array into 1 string by appending (,) or (/).
then i will store that in db (skill VARCHAR(1000))column.
taking size like this in db column is not such a good practise.
2) I have a plan!
I will take all checkbox names as columns in db like(C Boolean, C++ boolean, Java boolean.....).
then at the time of creating table i will make all their default value as false.
when the perticuler checkbox is checked then i will insert the value true as in db for example if user select checkbox java only then i will insert in db true as it's value.
"This seems very complex process"
from my perspective these 2 options are just tricky, but not an enterprise standard use!
Is there any way to store the String array in db other than like these stuppid tricks???
Is there any data type usage in db that takes our String[]
and gives back our string[] ??
if any?
please provide me with detailed steps!!
Thanks in advance!
Recently i find one solution: VARRAY in oracle db =>
SQL> CREATE OR REPLACE TYPE smoketype IS VARRAY(3) OF VARCHAR(30)
2 /
Type created.
SQL> CREATE TABLE register (fname VARCHAR(30), lname VARCHAR(30), gender VARCHAR
(1), smoke SMOKETYPE);
Table created.
SQL> INSERT INTO register VALUES
2 (
3 'omkar','t','m',
4 smoketype('no')
5 );
1 row created.
SQL> INSERT INTO register VALUES
2 (
3 'om','T','m',
4 smoketype('no','not at all')
5 );
1 row created.
SQL> INSERT INTO register VALUES
2 (
3 'onky','T','m',
4 smoketype('no','never','not at all')
5 );
1 row created.
with the above .. i created a type that takes multiple values just like an array.
but in my servlet when i create a statement and execute a selec * from register
query then i will get
the ResultSet obj!
but how could i retrieve that smoketype array into String[] back?
how could i retrieve the string[] values?
please someone help me?
Is every value in the string array an actual string, or just a flag of some sort? true/false etc
If it's just a flag you can store it as a mask with each bit corresponding to some specific checkbox,
For example, if you have 3 checkboxes x,y,z with values true/false, you can have the mask be 3 bits, such as 101 (deonoting x = 1, y = 0, z = 1).
It's not the most descriptive way to store it as you have to keep track of actually your mask means, but it saves more space than just concatenating every value of you array

Difference between two date/time fields - Lotus Notes

I have three editable date/time fields which the first two is (field1 and field2), style: Calendar/time control. Both of them are showing the time: hour and minutes, eg: 15:51.
The third field also (editable) which I want to show the difference between field1 and field2.
Eg: If field1 is 14:41 and field2 is 14:30, then field3 = 00:11. I've tried field1-field2 but isn't working. The form has automatic refresh fields property. Thanks!
Your third field needs to be computed, not editable.
If it HAS to be editable for some reason, and you want it to update when the other two fields are changed, do this:
Create a new field and make it computed-for-display and hidden. Give it a formula like this
#If(field1=null | field2=null; #Return(""); "");
seconds := field1-field2;
hours := #Integer(seconds/3600);
minutes := #Modulo(#Integer(seconds/60); 60);
output := #Right("00" + #Text(hours); 2) + ":" + #Right("00" + #Text(minutes); 2);
#setfield("field3"; output);
#Command([ViewRefreshFields]);
""
Phil
I wrote this code, much easier...
Fields 'StartTime' and 'EndTime':
Type Date/Time, use Calendar/Time control, set it to only display time.
Check the property "Run Exiting/OnChange events after value changes".
The Exiting event should look like this:
Sub Exiting(Source As Field)
Call UpdateDuration()
End Sub
Field 'Duration':
Editable text field, but hidden.
Field 'dspDuration':
Computed for display text field. Value is just "Duration" (no quotes).
Then add the following code to the forms Global section:
Sub UpdateDuration()
Dim ws As New NotesUIWorkspace
Dim uidoc As NotesUIDocument
Dim starttime As NotesDateTime
Dim endtime As NotesDateTime
Dim duration As Integer
Set uidoc = ws.CurrentDocument
'*** Exit if not both times are entered
If uidoc.FieldGetText("StartTime") = "" Then
Exit Sub
Elseif uidoc.FieldGetText("StartTime") = "" Then
Exit Sub
End If
'*** Calculate duration in seconds and update field
Set starttime = New NotesDateTime( uidoc.FieldGetText("StartTime") )
Set endtime = New NotesDateTime( uidoc.FieldGetText("EndTime") )
duration = endtime.TimeDifference( starttime )
Call uidoc.FieldSetText("Duration", Cstr(duration) )
Call uidoc.Refresh()
End Sub
That's it. Easy, isn't it? If you want to modify the output (duration), you can easily do that, perhaps change it into minutes by diving it by 60.
Make sure you are getting the difference between two date time fields. If you need to, you can use the #TextToTime formula to convert text to a datetime type.
Then just subtract the first date from the second date and you'll get the difference in seconds.
Then divide that by 60 to get the difference in minutes.

Using a Repeater with a dynamically generated table, ie, so unknown field names

I'm trying to produce a repeater showing amounts of money taken by various payment types into a table.
Payment types available come from a global settings file as an array, I am creating a dataTable by looping this list and extracting sales reports (there might be a more efficient way than this loop, but this is not my concern at the minute).
My question: How do I bind this to a repeater and display it when I dont necessarily know the table column names?
I've tried various methods to give the table a header row and give the columns numerical names from a for > next loop, but am either getting no results, or
System.Data.DataRowView' does not contain a property with the name '7'. < or whatever number
This is where I currently am:
EDIT: JUST REALISED MY CODE WAS AWFUL, SO UPDATED:
Dim paymentTable As New DataTable("paymentTable")
For j = 0 To UBound(paymentTypes)
Dim Type = Trim(paymentTypes(j))
Dim headers As DataColumn = New DataColumn(j.ToString)
paymentTable.Columns.Add(headers)
Next
Dim titleRow As DataRow = paymentTable.NewRow()
For k = 0 To UBound(paymentTypes)
Dim Type = Trim(paymentTypes(k))
titleRow.Item(k) = Type
Next
paymentTable.Rows.Add(titleRow)
Dim newRow As DataRow = paymentTable.NewRow()
For i = 0 To UBound(paymentTypes)
Dim Type = Trim(paymentTypes(i))
Try
newRow.Item(i) = '' GO OFF AND GET STUFF FROM DB
Catch
newRow.Item(i) = "0 "
End Try
Next
paymentTable.Rows.Add(newRow)
THIS EDITED CODE WORKS BUT I ONLY GET ONE ITEM
What I was hoping for would look something like:
card | cash | paypal ... etc (headings row)
£250 | £54 | £78 ... etc (values row)
Obviously there're a million ways this can be done, but this makes sense for my application, which has to be expandable and contractable depending on payment types available and this whole table needs to be repeated for multiple locations (also variable depending on who's viewing, and the number of locations in the system)
No, dont give up but just dont name columns by absolute number with no string before try
Dim headers As DataColumn = New DataColumn("col"+ j.ToString)

Resources