With a few modifications you can change every single field in the array simply by writing data to it and then save the array with the same file format (see the file "productsSave.dat" after running the snippet with the modified 1st record).
And -as all fields have the same structure- for simplification of typing the implementation of the properties can be done by a macro:
Code: Select all
#Macro declareproperty(fieldname, retType)
#If retType = "s"
Declare Property fieldname As String 'for reading
Declare Property fieldname(v As String) 'for writing
#ElseIf retType = "i"
Declare Property fieldname As Integer 'for reading
Declare Property fieldname(v As Integer) 'for writing
#EndIf
#EndMacro
#Macro MakeProperty(fieldname, start, length, retType)
#If retType = "s"
Property tProduct.fieldname As String 'for reading
Return Mid(this.record, start, length)
End Property
Property tProduct.fieldname(v As String) 'for writing
Mid(this.record, start, length) = Left(v + Space(length), length)
End Property
#ElseIf retType = "i"
Property tProduct.fieldname As Integer 'for reading
Return Val(LTrim(Mid(this.record, start, length), Chr(0)))
End Property
Property tProduct.fieldname(v As Integer) 'for writing
Mid(this.record, start, 4) = Right(String(4, Chr(0)) + Str(v), 4)
End Property
#EndIf
#EndMacro
Type tProduct
Dim record As String
declareproperty(BarcodeNumber, "s")
declareproperty(posdescription, "s")
declareproperty(salelocation, "s")
declareproperty(agerestricted, "i")
declareproperty(agelimit, "s")
declareproperty(pricetype, "s")
declareproperty(retailprice, "s")
declareproperty(vatcode, "s")
declareproperty(print_guarantee_message, "i")
declareproperty(print_guarantee_code, "s")
declareproperty(displaymessage, "i")
declareproperty(messagenumber, "s")
declareproperty(sendtoppr, "i")
declareproperty(requestserial, "i")
declareproperty(itemnotallowed, "i")
declareproperty(itemnotallowed_reason, "s")
declareproperty(restrict_product_qty, "i")
declareproperty(product_qty_allowed, "i")
declareproperty(discount_not_allowed, "i")
declareproperty(no_refund_allowed, "i")
declareproperty(ask_for_qty_before_selling, "i")
declareproperty(healthy_start_voucher_ok, "i")
End Type
MakeProperty(BarcodeNumber, 1, 13, "s")
MakeProperty(posdescription, 14, 30, "s")
MakeProperty(salelocation, 44, 4, "s")
MakeProperty(agerestricted, 48, 4, "i")
MakeProperty(agelimit, 52, 2, "s")
MakeProperty(pricetype, 54, 1, "s")
MakeProperty(retailprice, 55, 10, "s")
MakeProperty(vatcode, 65, 1, "s")
MakeProperty(print_guarantee_message, 66, 4, "i")
MakeProperty(print_guarantee_code, 70, 6, "s")
MakeProperty(displaymessage, 76, 4, "i")
MakeProperty(messagenumber, 80, 6, "s")
MakeProperty(sendtoppr, 86, 4, "i")
MakeProperty(requestserial, 90, 4, "i")
MakeProperty(itemnotallowed, 94, 4, "i")
MakeProperty(itemnotallowed_reason, 98, 255, "s")
MakeProperty(restrict_product_qty, 353, 4, "i")
MakeProperty(product_qty_allowed, 357, 4, "i")
MakeProperty(discount_not_allowed, 361, 4, "i")
MakeProperty(no_refund_allowed, 365, 4, "i")
MakeProperty(ask_for_qty_before_selling, 369, 4, "i")
MakeProperty(healthy_start_voucher_ok, 373, 4, "i")
ReDim As tProduct product(0)
'read product list into array
Open "products.dat" For Binary Access Read As #1
Do Until Eof(1)
ReDim Preserve product(UBound(product) + 1)
product(UBound(product)).record = Input(376, #1)
Loop
Close
'change product no. 1
product(1).BarcodeNumber = "12345"
product(1).posdescription = "WHISKY"
product(1).agerestricted = 18
'save product testfile
Open "productsSave.dat" For Output As #1
For x As Integer = 1 To UBound(product)
Print #1, product(x).record;
Next
Close
'print out product list
For x As Integer = 1 To UBound(product)
With product(x)
Print " BarcodeNumber ";.BarcodeNumber
Print " posdescription ";.posdescription
Print " salelocation ";.salelocation
Print " agerestricted ";.agerestricted
Print " agelimit ";.agelimit
Print " pricetype ";.pricetype
Print " retailprice ";.retailprice
Print " vatcode ";.vatcode
Print " print_guarantee_message ";.print_guarantee_message
Print " print_guarantee_code ";.print_guarantee_code
Print " displaymessage ";.displaymessage
Print " messagenumber ";.messagenumber
Print " sendtoppr ";.sendtoppr
Print " requestserial ";.requestserial
Print " itemnotallowed ";.itemnotallowed
Print " itemnotallowed_reason ";.itemnotallowed_reason
Print " restrict_product_qty ";.restrict_product_qty
Print " product_qty_allowed ";.product_qty_allowed
Print " discount_not_allowed ";.discount_not_allowed
Print " no_refund_allowed ";.no_refund_allowed
Print "ask_for_qty_before_selling ";.ask_for_qty_before_selling
Print " healthy_start_voucher_ok ";.healthy_start_voucher_ok
Print "--------------------------------------"
Sleep
End With
Next
? "OK"
Sleep
Edit: Code updated to fix space issue.