這是一個園長學習VFP(Visual Foxpro)的Blog歡迎同好一起來參與

星期二, 4月 20, 2010

如何檢查是否為正確的DBF檔

一般我們use非DBF時會跳出系統的錯誤訊息,但我們並非都需要這個訊息,
要檢視特定檔案是否為DBF檔,我使用Try …Catch To 語法來攔截系統錯誤
很多類似的檢測手法都可以當它做範本
範例如下:

*傳入要測試的dbf檔,測試是否可開啟,可傳回>0之筆數,否傳回-1
Function chk_dbf
    Lparameters p_bf,p_no_show_msg
    Local ret,walias,OERR
    ret=-1
    If !Empty(p_bf)
        If !File(p_bf)
            If At(p_bf,'\')>0    &&有路徑
                If Empty(p_no_show_msg)
                    =chk_err(p_bf+" 不存在!")
                Endif
                Return ret
            Endif
        Endif
        Try
            Select 0
            Use (p_bf) Shar Again
            walias=Alias()
        Catch To OERR
            If Empty(p_no_show_msg)
                =chk_err(p_bf+" 不是資料庫,或已損壞")
            Endif
        Endtry
        If Type("oErr")<>'O'
            If Used(walias)
                Select &walias
                ret=Reccount()
                Use
            Endif
        Endif
    Endif
    Return ret
Endfunc

沒有留言: