I have a VB6 app that has existed for more than 7 years to which I have a few clients using it for their accounting solutions. Now my problem is that a particular client has grown so big with a 15GB database size, a ledger database table of more than 4 million rows, customer size of more than 100,000, etc.
I have code in that app where customers are debited for a flat rate after checking some parameters, for a client with a small or average database. It works just fine, but for this particular client, this code sometimes does not even complete in 6 hours. I am sincerely looking for help in solving my Do
Until
loop headache. Is there a way to optimize this code and eliminate the Do
Until
statement or make it run a lot faster?
TT1 = "GOODS%"
GClass = "10"
Shuss = "Interest Declaration"
Set rst = HConn.Execute("Select * from tblCInterest where AccID like '" & TT1 & "' order by AccID")
If rst.BOF Then
MsgBox " There Is No Existing Account Under That Type", vbInformation
Exit Sub
Else
Set rstd = HConn.Execute("Select count(*) as Countss from tblCINT where AccID like '" & TT1 & "'")
If rstd.EOF Then
Else
Label6.Caption = rstd!Countss
End If
Set rstG = HConn.Execute("select Balw from tblSerialCount")
If rstG!Balw = "" Then
MsgBox " You Have Not Setup The Percentage", vbInformation
Exit Sub
Else
End If
Set rstB = HConn.Execute("select srefno from tblSerialNo2")
If rstB!srefno = 0 Then
MsgBox " You Have Not Setup The Parameter For Maximum Sales in a Month Per Customer !", vbInformation
Exit Sub
Else
End If
Set rst2 = HConn.Execute("Select * from tblSerialNumbers")
DPP = Val(rst2!Int1) + 1
Trump = "INT" & GClass & "/" & Val(rst2!Int1) + 1
DAF:
Set rst2T = HConn.Execute("Select * from tblLedger where GREF = '" & Trump & "'")
If rst2T.BOF Then
Else
DPP = Val(DPP) + 1
Trump = "INT" & GClass & "/" & DPP
GoTo DAF
End If
HConn.Execute ("update tblSerialNumbers set Int1 ='" & Val(DPP) & "'")
Muka = Trump
GD = Trump
OD1 = 0
On Error GoTo ErrHandler
Do Until rst.EOF
BAL1 = 0
BAL2 = 0
PBar.Value = PBar.Value + 1
'INT
Joe = Val(Trim(Text1.Text)) / 100
Joe6 = Val(rst!CustBaL1)
Joe2 = Round(((Joe * rst!CustBaL1) / 12), 2)
'WHT
If Val(rstG!Balw) > 0 Then
TanG = "Total Tax For " & Dew2 & " " & Year(Date)
Tan6 = Val(rstG!Balw) / 100
Tan5 = Round(Val(Joe2) * Tan6, 2)
Else
TanG = "Total Tax on Interest Paid For " & Dew2 & " " & Year(Date)
Tan5 = 0
End If
Set rstB = HConn.Execute("select srefno from tblSerialNo2")
If rstB!srefno = 0 Then
Else
Set rstc = HConn.Execute("select count(*) as Bety from tblSalesR where AccID = '" & rst!AccID & "' and Wmonth = '" & Joe4 & "' and Wyear = '" & Joe5 & "' ")
If rstc!bety > Val(rstB!srefno) Then
GoTo Dante
Else
End If
End If
Set rst15 = HConn.Execute("Select Blocked from tblClients where AccID = '" & rst!AccID & "'")
If rst15.BOF Then
GoTo Dante
Else
if rst15!Blocked = "YES" then
GoTo Dante
else
end if
end if
Set rst2 = HConn.Execute("Select * from tblClientInterest where Acctno = '" & rst!AccID & "' and period = '" & Month(Date) & "' and IntYear = '" & Year(Date) & "' ")
If rst2.BOF Then
Else
GoTo Dante
End If
Set rst15 = HConn.Execute("Select Bal101,CCode from tblClients where AccID = '" & rst!AccID & "'")
If rst15.BOF Then
GoTo Dante
Else
BAL1 = Val(rst15!Bal101)
'MINIMUM BALANCE SETUP
Set rstF = HConn.Execute("select Flopno from tblSerialNo2")
If Val(rstF!flopno) >= 0 Then
If Val(BAL1) < Val(rstF!flopno) Then
GoTo Dante
End If
Else
GoTo Dante
End If
Joe3 = Round((Val(rst15!Bal101) + Joe2) - Tan5, 2)
Joe4 = Month(Date)
Joe5 = Year(Date)
BranchT = rst15!CCode
End If
If Val(Joe2) < 0 Then
GoTo Dante
End If
'ELIMINATE ZERO BALANCE
If Val(BAL1) < 0 Then
GoTo Dante
End If
Set rst11 = HConn.Execute("select * from tblAcctClasses where Acct='" & DataCombo1.Text & "' and Bcode = '" & BranchT & "'")
If rst11.BOF Then
MsgBox " The Client Type Does Not Exist In The Database ! ", vbInformation
GoTo Dante
Else
TT11 = rst11!ACCTCODE
TUID = rst11!ACCTCODE
End If
GOVAcct = rst!AccID
GOVRef = Muka
'INT
DBAL1 = Round(Val(BAL1), 2)
HConn.Execute ("insert into tblLedger values ('" & Replace(rst!ClientName, "'", "''") & "','" & rst!AccID & "','" & Replace(DataCombo1, "'", "''") & "','" & Year(Date) & "','" & Date & "','" & Shuss & "','" & Muka & "','0','" & Val(Joe2) & "','0','0','0','','" & Replace(PostUser, "'", "''") & "','" & Format(time, "HH:MM:SS Am/Pm") & "')")
BAL2 = Round(Val(BAL1) + Val(Joe2), 2)
HConn.Execute ("Update tblClients set Bal101='" & BAL2 & "' where AccID = '" & rst!AccID & "'")
If Val(DBAL1) >= 0 Then
HConn.Execute ("insert into tblBookLedgerTemp values('" & Year(Date) & "','" & Date & "','" & TT11 & "','" & Replace(Shuss, "'", "''") & "','" & Muka & "','0','" & Val(Joe2) & "','" & Replace(PostUser, "'", "''") & "')")
ElseIf Val(DBAL1) < 0 Then
If Val(DBAL1) + Val(Joe2) <= 0 Then
HConn.Execute ("insert into tblBookLedgerTemp values('" & Year(Date) & "','" & Date & "','" & C5 & "','" & Replace(Shuss, "'", "''") & "','" & Muka & "','0','" & Val(Joe2) & "','" & Replace(PostUser, "'", "''") & "')")
GOVVal = Val(Joe2)
PERFCHECK4
Else
CBAL1 = Val(DBAL1) * -1
CBAL2 = Round(Val(Joe2) + Val(DBAL1), 2)
HConn.Execute ("insert into tblBookLedgerTemp values('" & Year(Date) & "','" & Date & "','" & TT11 & "','" & Replace(Shuss, "'", "''") & "','" & Muka & "','0','" & Val(CBAL2) & "','" & Replace(PostUser, "'", "''") & "')")
HConn.Execute ("insert into tblBookLedgerTemp values('" & Year(Date) & "','" & Date & "','" & C5 & "','" & Replace(Shuss, "'", "''") & "','" & Muka & "','0','" & Val(CBAL1) & "','" & Replace(PostUser, "'", "''") & "')")
GOVVal = Val(CBAL1)
PERFCHECK4
End If
End If
HConn.Execute ("insert into tblBookLedgerTemp values('" & Year(Date) & "','" & Date & "','" & C6 & "','" & Shuss & "','" & Muka & "','" & Val(Joe2) & "','0','" & Replace(PostUser, "'", "''") & "')")
'TAX
BAL1 = Val(CustBaL1(rst!AccID))
BalTax = Val(BAL1)
BalTax2 = Round(Val(BalTax) - Val(Tan5), 2)
HConn.Execute ("insert into tblLedger values ('" & Replace(rst!ClientName, "'", "''") & "','" & rst!AccID & "','" & Replace(DataCombo1, "'", "''") & "','" & Year(Date) & "','" & Date & "','" & TanG & "','" & Muka & "','" & Val(Tan5) & "','0','0','0','0','','" & Replace(PostUser, "'", "''") & "','" & Format(time, "HH:MM:SS Am/Pm") & "')")
BAL2 = Round(Val(BAL1) - Val(Tan5), 2)
HConn.Execute ("Update tblClients set Bal101='" & BAL2 & "' where AccID = '" & rst!AccID & "'")
If Val(BalTax) <= 0 Then
HConn.Execute ("insert into tblBookLedgerTemp values('" & Year(Date) & "','" & Date & "','" & C5 & "','" & TanG & "','" & Muka & "','" & Val(Tan5) & "','0','" & Replace(PostUser, "'", "''") & "')")
GOVVal = Val(Tan5)
PERFCHECK3
ElseIf Val(BalTax) > 0 And Val(BalTax2) < 0 Then
CBAL1 = Val(BalTax)
CBAL2 = Round(Val(Tan5) - Val(CBAL1), 2)
HConn.Execute ("insert into tblBookLedgerTemp values('" & Year(Date) & "','" & Date & "','" & TT11 & "','" & TanG & "','" & Muka & "','" & Val(CBAL1) & "','0','" & Replace(PostUser, "'", "''") & "')")
HConn.Execute ("insert into tblBookLedgerTemp values('" & Year(Date) & "','" & Date & "','" & C5 & "','" & TanG & "','" & Muka & "','" & Val(CBAL2) & "','0','" & Replace(PostUser, "'", "''") & "')")
GOVVal = Val(CBAL2)
PERFCHECK3
Else
HConn.Execute ("insert into tblBookLedgerTemp values('" & Year(Date) & "','" & Date & "','" & TT11 & "','" & TanG & "','" & Muka & "','" & Val(Tan5) & "','0','" & Replace(PostUser, "'", "''") & "')")
End If
HConn.Execute ("insert into tblBookLedgerTemp values('" & Year(Date) & "','" & Date & "','" & C24 & "','" & TanG & "','" & Muka & "','0','" & Val(Tan5) & "','" & Replace(PostUser, "'", "''") & "')")
HConn.Execute ("insert into tblClientInterest values ('" & Replace(rst!ClientName, "'", "''") & "','" & rst!AccID & "','" & Date & "','" & Joe2 & "','" & Joe4 & "','" & Year(Date) & "','" & Replace(PostUser, "'", "''") & "','" & Muka & "','')")
Dante:
rst.MoveNext
Loop
HConn.Execute ("insert into tblClientInterestdone values('" & Month(Date) & "','" & Year(Date) & "','" & Replace(PostUser, "'", "''") & "','" & DataCombo1.Text & "')")
End If
'SECOND SET
Set rst15 = HConn.Execute("Select distinct AccID as AccID from tblBookLedgerTemp order by AccID ")
If rst.BOF Then
Else
Do Until rst15.EOF
Set rst16 = HConn.Execute("Select sum(credit) - sum(debit) as Bal from tblBookLedgerTemp where AccID = '" & rst15!AccID & "'")
AB1 = Round(IIf(IsNull(rst16!Bal), 0, rst16!Bal), 2)
Set rst2 = HConn.Execute("Select * from tblBookLedgerTemp Where AccID = '" & rst15!AccID & "' ")
Shuss = Mudas '& " " & DataCombo1.Text
If Val(AB1) > 0 Then
HConn.Execute ("Insert into tblBookLedger values ('" & rst2!gPeriod & "','" & rst2!PostDate & "','" & rst15!AccID & "','" & Replace(Shuss, "'", "''") & "','" & rst2!GREF & "','0','" & Val(AB1) & "','" & rst2!Puser & "')")
ElseIf Val(AB1) < 0 Then
HConn.Execute ("Insert into tblBookLedger values ('" & rst2!gPeriod & "','" & rst2!PostDate & "','" & rst15!AccID & "','" & Replace(Shuss, "'", "''") & "','" & rst2!GREF & "','" & Val(AB1) * -1 & "','0','" & rst2!Puser & "')")
Else
End If
rst15.MoveNext
Loop
End If
MsgBox " The Interest Have Been Completed !", vbInformation