Question

Currently based on some advice from experts, 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):

Set myR = CurrentDb.OpenRecordset("tbl_FFIFinancials")
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

myR.Close

Set myR = Nothing

Similar code is for edit and add method (putting back normalized data)

 Set myR = CurrentDb.OpenRecordset("tbl_FFIFinancials")

'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

myR.Close

Set myR = Nothing

I believe there is some way of optimizing the code by using shortcut methods like do while loop or for next methods. As i am just a novice in vba programming I feel it hard to grasp how actually do it, and have to repeat code line several times.


Update: I tried I tried this one to achive optimization of code but it retuns mismatch error on my new custom function:

Dim myR As DAO.Recordset
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 ....

Function MyFieldName(Name As Field)
Dim strName As String

srtName = "PrevPerItem" & byteItemNumber
Name.Name = strName

End Function
Was it helpful?

Solution

Found a solution elsewhere. Generally it states the following: The field on the form can be referred as 1. Me.YourTextboxName.Value=myR.Fields("Amount").Va lue

2. Me(Textbox).Value =myR.Fields("Amount").Value

the second one is useful if you have a series of textboxes and value is to be assigned dynamically like if you have 17 textboxes named TxtBox1,TxtBox2 and TxtBox3 ... TextBox17 then

for i = 1 to 17    Me("TxtBox" & i).value=myR.Fields("Amount").Value
next i 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top