The structure of the program is that from the main module I try to count the number of boxes of a btr table. I have put the btr functions (open, movefirst, movenext, stopclient, closefile, recordcount) in a class module that returns the key of the record just read and gives the data in a property called data.
The form in the main module is loaded to see something when it is run as an exe file.
I have attached the code as well in a zip file.
Here you can find the code:
This is the main module:
Code:
Dim CodigoLinea As String
Dim TV As New TablaVel
Dim rs(20) As String
Dim lc(20) As Integer
Dim NumCajas As Long
Dim Producto As String
Public Sub Main()
On Error GoTo ErrMn
Dim i As Integer
Dim msg As String
CodigoLinea = "01"
frmEfiPro.Show
IniCampos
With TV
' ****taFichero = "\\esmlsv303\ConPro\ConPro"
****taFichero = "C:\Probas Betrieve\ConPro"
.NombreFichero = "hpco" + VBA.Format$(Now, "YY") + CodigoLinea + ".btr"
.ClavePpal = 4
If Not .AbreFichero Then
.StopClient
End
End If
' MsgBox "Recordcount: " & VBA.Format(TablaVel.RecordCount, "#,###"), vbInformation, "Info"
NumCajas = 0
Producto = ""
msg = "Fecha y hora iniciales: 20060305 06:00:00" & VBA.Chr(10) & _
"Fecha y hora finales: 20060312 18:00:00" & VBA.Chr(10)
While ContarCajasProducto("20060307", "06:00:00", "20060312", "18:00:00")
msg = msg & VBA.Chr(10) & "Producto: " & Producto & " Cajas: " & NumCajas
Wend
msg = msg & VBA.Chr(10) & "Producto: " & Producto & " Cajas: " & NumCajas
MsgBox msg
.StopClient
.CloseFile
End With
End
ErrMn:
MsgBox "Error en Main: " & Chr(10) & Err.Description & Chr(10) & _
vbCritical, "Error: " & Err.Number
End
End Sub
Private Sub IniCampos()
lc(0) = 8: lc(1) = 20:
lc(2) = 8: lc(3) = 8:
lc(4) = 8: lc(5) = 8:
lc(6) = 2: lc(7) = 3:
lc(8) = 10: lc(9) = 2:
lc(10) = 20: lc(11) = 1:
lc(12) = 2: lc(13) = 8:
lc(14) = 10: lc(15) = 16:
lc(16) = 1: lc(17) = 4:
lc(18) = 5: lc(19) = 10
End Sub
Public Function ContarCajasProducto(FechaIni As String, HoraIni As String, FechaFin As String, HoraFin As String) As Boolean
On Error GoTo ErrCC
Dim Key As String
Dim FechaHoraFin As Date
Key = 0
NumCajas = 0
While Key <> "-1"
If Producto = "" Then
Key = TV.PrimerRegistro("N" + FechaIni + HoraIni)
Else
Producto = rs(TVfld.Producto)
Key = TV.SiguienteRegistro
End If
If Key = "-1" Then 'EOF
ContarCajasProducto = False
Exit Function
ElseIf Key = "" Then
End
Else
Buf_Mem TV.Data
If Producto = "" Then
Producto = rs(TVfld.Producto)
End If
FechaHoraFin = "N" + FechaFin + HoraFin
If StrComp(Key, FechaHoraFin, vbTextCompare) = 1 Then
ContarCajasProducto = False
Exit Function
ElseIf rs(TVfld.Producto) <> Producto Then 'Producto diferente
ContarCajasProducto = True
Producto = rs(TVfld.Producto)
Else 'Seguir contando
NumCajas = NumCajas + Val(rs(TVfld.NCajas))
End If
End If
Wend
Exit Function
ErrCC:
MsgBox "Error en ContarCajas: " & Chr(10) & Err.Description & Chr(10) & _
"Data: " & Resultado, vbCritical, "Error: " & Err.Number
End
End Function
Private Sub Buf_Mem(Data As String)
'Separa los datos de la variable Dat$ en los campos correspondientes y los pone en Var(I)
On Error GoTo ErrBM
Dim posi As Integer
Dim i As Integer
For i = 0 To 19
rs(i) = Space(lc(i))
Next
posi = 1
For i = 0 To 19
rs(i) = Mid$(Data, posi, lc(i))
posi = posi + lc(i)
Next i
Exit Sub
ErrBM:
MsgBox "Error en Buf_Mem: " & Chr(10) & Err.Description & Chr(10) & _
"Data: " & Data & Chr(10) & "i: " & i & "; posi: " & posi & "; rs(i): " & rs(i) & "; lc(i): " & lc(i), _
vbCritical, "Error: " & Err.Number
End
End Sub
And this is the class TablaVel, where the btr file is read (just posted what it crashes because if not the post was too long):
Code:
DefInt A-Z
Dim DataBuf As CustRowType
Const DEFKeyBufLen = 255
Private Declare Function BTRCALL Lib "wbtrv32.dll" (ByVal OP, ByVal Pb$, Db As Any, DL As Integer, Kb As Any, ByVal Kl, ByVal Kn) As Integer
Public NombreFichero As String
Public RutaFichero As String
Public FicheroAbierto As Boolean
Public ClavePpal As Integer
Dim KeyBufLen As Integer
Dim PosBlk$
Dim DBLen As Integer
Dim KeyNum As Integer
Dim KeyBuffer$
Property Get Data() As String
Dim b As Variant
Data = ""
For Each b In DataBuf.buf
Data = Data & Chr(b)
Next
End Property
Public Function PrimerRegistro(Clave As String) As String
Dim stat As Integer
DBLen = Len(DataBuf)
KeyBuffer$ = Clave
KeyBufLen = Len(KeyBuffer$)
KeyNum = ClavePpal
stat = BTRCALL(Btr.GetGreaterEqual, PosBlk$, DataBuf, DBLen, ByVal KeyBuffer$, KeyBufLen, KeyNum)
If stat <> 0 Then
ErrBtr stat, "Error en el método MoveFirst de la clase FicheroBtr.", , vbExclamation, "Error"
Else
PrimerRegistro = KeyBuffer$
End If
End Function
Public Function SiguienteRegistro() As String
Dim stat As Integer
stat = 0
DBLen = Len(DataBuf)
KeyBufLen = DEFKeyBufLen 'Len(KeyBuffer$)
KeyNum = ClavePpal
stat = BTRCALL(Btr.GetNext, PosBlk$, DataBuf, DBLen, ByVal KeyBuffer$, KeyBufLen, KeyNum)
Select Case stat
Case 9
SiguienteRegistro = "-1"
Exit Function
Case 0
SiguienteRegistro = KeyBuffer$
Case Else
ErrBtr stat, "Error en el método MoveFirst de la clase FicheroBtr.", , vbExclamation, "Error"
End Select
End Function
And here you can find part of the global definitons (the post was too long):
Code:
'Enums globales
Public Enum TVfld
N_Registro = 0 ' As String * 8
Producto = 1 'As String * 20
Fecha = 2 'As String * 8
Hora = 3 'As String * 8
VelTeorica = 4 'As String * 8
VelReal = 5 'As String * 8
OperaLinea = 6 'As String * 2
Maquina = 7 'As String * 3
CanalCuenteo = 8 'As String * 10
CodContador = 9 'As String * 2
ObjContado = 10 'As String * 20
Rto_SN = 11 'As String * 1
Linea = 12 'As String * 2
ValAnterior = 13 'As String * 8
NLote = 14 'As String * 10
FechaHora = 15 'As String * 16
Enviado = 16 'As String * 1
TmpoComputado = 17 'As String * 4
NCajas = 18 'As String * 5
Libres = 19 'As String * 10
End Enum
There are other functions you can find in the zip file, as the error management or date conversion.