in one form I try present editable cross-tab query like form for displaying values in several columns from flat table (the data is financial data from various periods = FFI_Period for each financial statement item =FinStmtItem). I first denormalize the table by retrieving required values from table onto the unbound fields on the form (via recordset.findfirst method and then assigning value from table field onto the unbound text field) and then save the fields back (via recordset.edit/.add method) to the table in normalized form. One piece of code keeps repeating several times in my procedure. Here is an example of retrieving data from flat table by denormalizing it (*PrevPerItem*X - is the name of the unbound text field on the form):
Code:
Set myR = CurrentDb.OpenRecordset("tbl_FFIFinancials", dbOpenDynaset)
myR.FindFirst ("[FFI_PERIOD] =" & intFFIPeriod1 & "And [FinStmtItem] = 1")
PrevPerItem1 = myR.Fields("Amount").Value
myR.FindFirst ("[FFI_PERIOD] =" & intFFIPeriod1 & "And [FinStmtItem] = 2")
PrevPerItem2 = myR.Fields("Amount").Value
myR.FindFirst ("[FFI_PERIOD] =" & intFFIPeriod1 & "And [FinStmtItem] = 3")
PrevPerItem3 = myR.Fields("Amount").Value
'.... [similar codes repeats here as well from No 4 throu 16]............
myR.FindFirst ("[FFI_PERIOD] =" & intFFIPeriod1 & "And [FinStmtItem] = 17")
PrevPerItem17 = myR.Fields("Amount").Value
Similar code is for edit and add method (putting back normalized data)
Code:
Set myR = CurrentDb.OpenRecordset("tbl_FFIFinancials", dbOpenDynaset)
'updating values for the previous period
MsgBox "Updating values into Database for the period - " & intPrevYear & "-Q" & byteSelectQuarter
myR.FindFirst ("[FFI_PERIOD] =" & intFFIPeriod1 & "And [FinStmtItem] = 1")
myR.Edit
myR.Fields("Amount").Value = PrevPerItem1
myR.Update
myR.FindFirst ("[FFI_PERIOD] =" & intFFIPeriod1 & "And [FinStmtItem] = 2")
myR.Edit
myR.Fields("Amount").Value = PrevPerItem2
myR.Update
myR.FindFirst ("[FFI_PERIOD] =" & intFFIPeriod1 & "And [FinStmtItem] = 3")
myR.Edit
myR.Fields("Amount").Value = PrevPerItem3
myR.Update
'....[repeating code for values No 4 though 16]
myR.FindFirst ("[FFI_PERIOD] =" & intFFIPeriod1 & "And [FinStmtItem] = 17")
myR.Edit
myR.Fields("Amount").Value = PrevPerItem17
myR.Update
As names of text box fields are organized in some order, I believe there is some way to optimize this code, via referencing textbox name derived from some string variable.
At first, I tried I tried this one to achive optimization of code but it retuns mismatch error on my new custom function:
Code:
Set myR = CurrentDb.OpenRecordset("tbl_FFIFinancials", dbOpenDynaset)
For byteItemNumber = 1 To 17 Step 1
myR.FindFirst "[FFI_PERIOD] =" & intFFIPeriod1 & "And [FinStmtItem] =" & byteItemNumber
Me.MyFieldName(Name).Value = myR.Fields("Amount").Value
Next byteItemNumber
.... my new custom function to reference textfield name on the current form ....
Code:
Function MyFieldName(Name As Field)
Dim strName As String
srtName = "PrevPerItem" & byteItemNumber
Name.Name = strName
End Function
This is my first project in Access and first experience in vba coding, so I am completely lost here. Plz help me out.