Hello all, I'm new to this forum, hopefully somebody can help me out with a couple minor problems I have with my code.

I have data inputed into a flexgrid, and would like to sort it by YEAR in increasing order. Year is (frmStats.flexGrid.Col = 0).

The other problem I'm having is that when I try to get a batting average for players or their slugging percentage I get "0" the only time I don't get that is when the number of at bats is 0 then I display ".000", I check for division by zero to stop a run time exectution error. Below is the code you need to help.

Thank you for your help!

blkshirt

Code:
Option Explicit


 Type Demo_Record
    LahmanID As String * 9
    LastName As String * 20
    FirstName As String * 15
    Bats As String * 1
    Throws As String * 1
    BirthMonth As Byte
    BirthDay As Byte
    BirthYear As Integer
    RookieSeason As Integer
    FirstStatsRecord As Long
    StatsRecordPosition As Byte
End Type

Type Stats_In
    LahmanID As String * 9
    Year As Integer
    Team As String * 4
    League As String * 4
    Games As Integer
    AtBats As Integer
    Runs As Integer
    Hits As Integer
    Doubles As Integer
    Triples As Integer
    HomeRuns As Integer
    RBIs As Integer
    SacHits As Integer
    SacFlies As Integer
    Steals As Integer
    Caught As Integer
    Walks As Integer
    Intentional As Integer
    HitByPitch As Integer
    StrikeOuts As Integer
    NextStatsRecord As Long
    StatsRecordPosition As Byte
End Type

Type Stats_Block
    Stats(1 To 5) As Stats_In
End Type

Type Count_Em_Block
    NumStats As Long
    x As String * 276
End Type

Global K As String
Global i As Integer
Global J As Integer

Global DemoFileName As String
Global StatsFileName As String

Global DemoRecord As Demo_Record
Global StatsBlock As Stats_Block
Global StatsIn As Stats_In
Global CountBlock As Count_Em_Block
Global PD As Stats_Block

Global Year As Integer
Global Team As String
Global Leauge As String
Global Games As Integer
Global Runs As Integer
Global AtBats As Integer
Global Hits As Integer
Global Doubles As Integer
Global Triples As Integer
Global HomeRuns As Integer
Global RBIs As Integer
Global SacHits As Integer
Global SacFlies As Integer
Global Steals As Integer
Global Caught As Integer
Global Walks As Integer
Global Intentional As Integer
Global HitByPitch As Integer
Global StrikeOuts As Integer

Global CurrentPlayer As Integer
Global NumSeasons As Integer
Global NumPlayers As Integer
Global FullName As String

Global numRecord As Integer
Global ApproxRecords As Long
Global RecsRead As Long
Global StatsRead As Integer
Global StatsRecord As Integer

Global YearRow As Integer
Global TeamRow As Integer
Global LeaugeRow As Integer
Global GamesRow As Integer
Global ABRow As Integer
Global HitRow As Integer
Global DoubleRow As Integer
Global TripleRow As Integer
Global HRRow As Integer
Global RBIRow As Integer
Global SacHRow As Integer
Global SacFRow As Integer
Global StealsRow As Integer
Global WalksRow As Integer
Global HitByRow As Integer
Global KRow As Integer
Global BARow As Integer
Global SPRow As Integer
Global TBRow As Integer
Global SingleRow As Integer
Global BA As Integer
Global SP As Integer
Global TB As Integer
Global firstbase As Integer




'Function used to Calculate a Players Batting Average
Function BatAvg()
    
    If Val(AtBats) = 0 Then
        frmStats.lblBA.Caption = ".000"
    Else
        BatAvg = Round(Val(Hits) / Val(AtBats), 3)
    End If
    
End Function
'Function used to Calculate a Players Total Base Count (Used in Slugging Percentage)
Function TotalBases()
   TotalBases = Val(Singles) + Val(Doubles * 2) + Val(Triples * 3) + Val(HomeRuns * 4)
End Function
'Function used to Calculate a Players Slugging Percentage
Function SluggingPct()
    
    If Val(AtBats) = 0 Then
        frmStats.lblSP.Caption = ".000"
    Else
        SluggingPct = Round(Val(TotalBases) / Val(AtBats), 3)
    End If
    
End Function
'Function used to Calculate a Players Total of Singles as it is not given in the data file.
Function Singles()
    Singles = Val(Hits) - Val(Doubles) - Val(Triples) - Val(HomeRuns)
End Function
Public Function BinSearch(ByVal Wanted As String, ByVal Lo As Integer, ByVal Up As Integer) As Integer

'*****************************************************
'*                  Search Function                  *
'*****************************************************


    If Lo > Up Then
        BinSearch = False
        MsgBox ("Player Not Found")
    Else
        i = (Lo + Up) / 2
        
        If Left(frmStats.cbo1.List(i), Len(Wanted)) = Wanted Then
            BinSearch = True
            frmStats.hsbScroll.Value = i
        Else
            If Left(frmStats.cbo1.List(i), Len(Wanted)) > Wanted Then
                BinSearch = BinSearch(Wanted, Lo, i - 1)
            Else
                BinSearch = BinSearch(Wanted, i + 1, Up)
            End If
        End If
      End If
End Function