home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / SQL Query 20519632001.psc / modCommon.bas < prev    next >
Encoding:
BASIC Source File  |  2000-12-14  |  14.8 KB  |  393 lines

  1. Attribute VB_Name = "modCommon"
  2. Option Explicit
  3.  
  4.  
  5. Public gbErrorHandSwitch As Boolean
  6.  
  7. Public gsUserName As String
  8.  
  9. Public Enum gDatabaseType
  10.     gdbtAccess
  11.     gdbtSQLServer
  12. End Enum
  13.  
  14. Public Enum gDriverList
  15.     gAll
  16.     gLocal
  17. End Enum
  18.  
  19. Public Enum gFormSettingType
  20.     gfstAll
  21.     gfstPositionOnly
  22.     gfstSizeOnly
  23. End Enum
  24.  
  25.  
  26. '------------------------------------------------------------------------------
  27. 'Description:   Loads a forms top, left, height and width from a sub key under
  28. '               the default application area.
  29. '               Also ensures that some part of the form is visible in case the
  30. '               user changes screen resolution.
  31. 'Parameters:    frm - the form to load.
  32. '               fst - the type of settings to load.
  33. '
  34. 'Example Usage:
  35. 'qLoadFormSettings Me
  36. 'qLoadFormSettings Me, gfstPositionOnly
  37. '------------------------------------------------------------------------------
  38. '
  39. Public Sub LoadFormSettings(frm As Form, Optional fst As gFormSettingType = gfstAll, Optional bChangeColours As Boolean = True, Optional bCheckTag As Boolean = False)
  40. If gbErrorHandSwitch Then On Error GoTo ErrHandler
  41.     Dim lWindow As Long
  42.     Dim sWindow As String
  43.     Dim i As Integer, s() As String, sStr As String
  44.     Dim ctl As Control
  45.     Const sNOT_SET As String = "not"
  46.     Const iTOP = 0
  47.     Const iLEFT = 1
  48.     Const iHEIGHT = 2
  49.     Const iWIDTH = 3
  50.     
  51.     sWindow = "0,0," & frm.Height & "," & frm.Width & ","
  52.     sStr = GetSetting(gsApplName, "Forms", frm.Name, sWindow)
  53.     s = Split(sStr, ",")
  54.     
  55. '    frm.Top = s(iTOP)
  56. '    frm.Left = s(iLEFT)
  57.     
  58.     ' only position and size the form if it has been set and is in a normal state
  59.         
  60.     If fst = gfstAll Or fst = gfstSizeOnly Then
  61.         If UBound(s()) >= iHEIGHT Then If Val(s(iHEIGHT)) > 0 Then frm.Height = Val(s(iHEIGHT))
  62.         If UBound(s()) >= iWIDTH Then If Val(s(iWIDTH)) > 0 Then frm.Width = Val(s(iWIDTH))
  63.     End If
  64.     
  65.     If UBound(s()) >= iTOP Then
  66.         If Val(s(iTOP)) > 0 And Val(s(iTOP)) < Screen.Height Then
  67.             frm.Top = Val(s(iTOP))
  68.         Else
  69.             frm.Top = 0
  70.         End If
  71.     End If
  72.     
  73.     If UBound(s()) >= iLEFT Then
  74.         If Val(s(iLEFT)) > 0 And Val(s(iLEFT)) < Screen.Width Then
  75.             frm.Left = Val(s(iLEFT))
  76.         Else
  77.             frm.Left = 0
  78.         End If
  79.     End If
  80.     ' ensure the form is in the visible area of the screen
  81.     If frm.Top > Screen.Height - frm.Height Then frm.Top = 0
  82.     If frm.Top < 0 Then frm.Top = 0
  83.     If frm.Left > (Screen.Width - frm.Width) Then frm.Left = 0
  84.     If frm.Left < 0 Then frm.Left = 0
  85.     
  86.     Clear frm
  87.  
  88. ErrExit:      Exit Sub
  89. ErrHandler:   Call ErrorHandler("VB5Common", 0, "LoadFormSettings")
  90. End Sub
  91. '------------------------------------------------------------------------------
  92. 'Description:   Return the application version in a standard format, ie
  93. '               M.mm.rrrr
  94. '               where   M = App.Major
  95. '                       m = App.Minor
  96. '                       r = App.Revision
  97. '
  98. 'Example Usage:
  99. 'sString = qGetAppVersion()
  100. '------------------------------------------------------------------------------
  101. '
  102. Public Function GetAppVersion() As String
  103. If gbErrorHandSwitch Then On Error GoTo ErrHandler
  104.     GetAppVersion = App.Major & "." & Format$(App.Minor, "00") & "." & Format$(App.Revision, "0000")
  105. ErrExit:         Exit Function
  106. ErrHandler:      Call ErrorHandler("VB5Common", 0, "GetAppVersion")
  107. End Function
  108. '=====================================================================
  109. 'Description:       Converts Date into American Format
  110. 'Parameters         d           - Date
  111. '                   dbt         - Database Format
  112. '
  113. Public Function AMDate(d As Date, Optional dbt As gDatabaseType = gdbtSQLServer) As String
  114. If gbErrorHandSwitch Then On Error GoTo ErrHandler
  115.     If dbt = gdbtSQLServer Then
  116.         AMDate = "'" & Month(d) & "/" & Day(d) & "/" & Year(d) & "'"
  117.     Else
  118.         AMDate = "#" & Month(d) & "/" & Day(d) & "/" & Year(d) & "#"
  119.     End If
  120. ErrExit:         Exit Function
  121. ErrHandler:      Call ErrorHandler("VB5Common", 0, "AMDate")
  122. End Function
  123. '=====================================================================
  124. 'Description:       Converts Date/Time into American Format
  125. 'Parameters         d           - Date
  126. '                   dbt         - Database Format
  127. '
  128. Public Function AMDateTime(ByVal d As Date, Optional dbt As gDatabaseType = gdbtSQLServer) As String
  129. If gbErrorHandSwitch Then On Error GoTo ErrHandler
  130.     If dbt = gdbtSQLServer Then
  131.         AMDateTime = "'" & Month(d) & "/" & Day(d) & "/" & Year(d) & " " & TimeValue(d) & "'"
  132.     Else
  133.         AMDateTime = "#" & Month(d) & "/" & Day(d) & "/" & Year(d) & " " & TimeValue(d) & "#"
  134.     End If
  135. ErrExit:         Exit Function
  136. ErrHandler:      Call ErrorHandler("VB5Common", 0, "AMDateTime")
  137. End Function
  138. '=====================================================================
  139. 'Description:       Converts Date into American Format
  140. 'Parameters         dbt       - Database Format
  141. '
  142. Public Function AMTS(Optional dbt As gDatabaseType = gdbtSQLServer) As String
  143. If gbErrorHandSwitch Then On Error GoTo ErrHandler
  144.     If dbt = gdbtSQLServer Then
  145.         AMTS = "'" & Month(Date) & "/" & Day(Date) & "/" & Year(Date) & " " & TimeValue(Time) & "'"
  146.     Else
  147.         AMTS = "#" & Month(Date) & "/" & Day(Date) & "/" & Year(Date) & " " & TimeValue(Time) & "#"
  148.     End If
  149. ErrExit:         Exit Function
  150. ErrHandler:      Call ErrorHandler("VB5Common", 0, "AMTS")
  151. End Function
  152.  
  153. '=====================================================================
  154. 'Description:       Converts Time into American Standard
  155. 'Parameters         t       - Time
  156. '                   dbt     - Database Format
  157. '
  158. Public Function AMTime(ByVal t As Date, Optional dbt As gDatabaseType = gdbtSQLServer) As String
  159. If gbErrorHandSwitch Then On Error GoTo ErrHandler
  160.     If dbt = gdbtSQLServer Then
  161.         AMTime = "'" & TimeValue(t) & "'"
  162.     Else
  163.         AMTime = "#" & TimeValue(t) & "#"
  164.     End If
  165. ErrExit:         Exit Function
  166. ErrHandler:      Call ErrorHandler("VB5Common", 0, "AMTime")
  167. End Function
  168.  
  169. '=====================================================================
  170. 'Description:       Executes SQL Query
  171. 'Parameters         db              - Database
  172. '                   sSql            - SQL String
  173. '                   dbt         - Database Format
  174. 'GLOBALs            gQLogInd        - Indicator 0- (Default) no Log; 1-Print Log for ALL SQL
  175. '                   gQLogPath       - QLogPath
  176. '
  177. Public Sub SQLExecute(DB As ADODB.Connection, sSql As String, Optional dbt As gDatabaseType = gdbtSQLServer)
  178. If gbErrorHandSwitch Then On Error GoTo ErrHandler
  179.     Dim TimerStart As Double, TimerTotal As Double, sPrintLine As String
  180.     TimerStart = Timer
  181.         
  182.     If DB.State = adStateOpen Then
  183. '        DB.BeginTrans
  184.         DB.Execute sSql, , adCmdText + adExecuteNoRecords
  185. '        DB.CommitTrans
  186.     End If
  187.     
  188.  
  189. rrExit:      Exit Sub
  190. ErrHandler:   Call ErrorHandler("VB5Common", 0, "SQLExecute", sSql)
  191. End Sub
  192. '=====================================================================
  193. 'Description:       Executes SQL Query and Creates ADO Recordset
  194. 'Parameters         db              - Database
  195. '                   sSql            - SQL String
  196. 'GLOBALs            gQLogInd        - Indicator 0- (Default) no Log; 1-Print Log for ALL SQL
  197. '                   gQLogPath       - QLogPath
  198. '
  199. Public Function SQLOpenRecordset(DB As ADODB.Connection, sSql As String) As ADODB.Recordset
  200. If gbErrorHandSwitch Then On Error GoTo ErrHandler
  201.     Dim l As Long
  202.     Dim TimerStart As Double, TimerTotal As Double, sPrintLine As String
  203.     TimerStart = Timer
  204.     
  205.     If DB.State = adStateOpen Then
  206.         Set SQLOpenRecordset = DB.Execute(sSql, , adCmdText)
  207.     End If
  208.     
  209. ErrExit:         Exit Function
  210. ErrHandler:      Call ErrorHandler("VB5Common", 0, "SQLOpenRecordset", sSql)
  211. End Function
  212.  
  213. '=======================================================
  214. '   System Version in format V.0.0.0000
  215. '
  216. '
  217. Public Function SystemVersion() As String
  218. If gbErrorHandSwitch Then On Error GoTo ErrHandler
  219.     SystemVersion = "V." & App.Major & "." & App.Minor & "." & App.Revision
  220. ErrExit:         Exit Function
  221. ErrHandler:      Call ErrorHandler("VB5Common", 0, "SystemVersion")
  222. End Function
  223.  
  224.  
  225. '========================================================================
  226. 'Description:   Error Handler Process and generate Error messages in Log files
  227. 'PARAMETERS:    sFormName       - Form Name
  228. '               ind             - Group or line Index variable (used mainly if Subs with Index parameter
  229. '               SubName         - Procedure number withing a Form
  230. '               sSql            - SQl Statement or another String passed from the form
  231. '               bLogOnly        - Put only in Database Log
  232. '
  233. '
  234. Public Sub ErrorHandler(sFormName As String, ind As Integer, SubName As String, Optional sSql As String, Optional bLogOnly As Boolean)
  235.     Dim iFreeFileN As Integer    ' Next Free File Number
  236.     Dim sPrintLine As String     ' Line to be printed
  237.     Dim sLogName As String       ' Log file name
  238.     Dim lErrNum As Long, sErrDesc As String, sErrSource As String
  239.                 
  240.     ' Show Indicator
  241.     Screen.MousePointer = vbDefault
  242.     sErrDesc = Err.Description
  243.     lErrNum = Err.Number
  244.     sErrSource = Err.Source
  245.     sPrintLine = SystemVersion & " - " & " - " & sFormName & "  -  " & SubName & " - Section - " & CStr(ind) & " - " & sErrDesc & " Error # " & lErrNum & "  " & sErrSource & " " & sSql
  246.     
  247.     'Forming Log File Name and open it
  248.     If Dir(gsPathLogs, vbDirectory) = "" Or gsPathLogs = "" Then gsPathLogs = App.Path & "\"
  249.     sLogName = gsPathLogs & "log" & Year(Date) & Month(Date) & Day(Date) & ".txt"
  250.     
  251.     iFreeFileN = FreeFile
  252.     Open sLogName For Append Shared As #iFreeFileN
  253.     ' Printing Error Message
  254.     Print #iFreeFileN, sPrintLine, Time
  255.     MsgBox sPrintLine, , "ERROR MESSAGE"
  256.     Close #iFreeFileN
  257.     
  258. End Sub
  259.  
  260.  
  261. '================================================================================
  262. 'Description:   Converts SQL String into The appropriate format
  263. 'PARAMETERS:    sStr - Source String
  264. '
  265. '
  266. Public Function SQLCheck(sStr As String) As String
  267. If gbErrorHandSwitch Then On Error GoTo ErrHandler
  268.     Dim s As String
  269.     s = Replace(sStr, "'", "''")
  270.     s = Replace(s, "", """")
  271.     SQLCheck = "'" & s & "'"
  272. ErrExit:         Exit Function
  273. ErrHandler:      Call ErrorHandler("VB5Common", 0, "SQLCheck")
  274. End Function
  275.  
  276. '=================================================================
  277. ' SUB - Cleanup ALL controls on the form and Setup TDBGrid
  278. ' PARAMETERS:   frm         - Form Object
  279. '
  280. '
  281. Public Sub Clear(frm As Form)
  282. If gbErrorHandSwitch Then On Error GoTo ErrHandler
  283.     Dim ctl As Control, a
  284.     For Each ctl In frm
  285.         Select Case UCase(TypeName(ctl))
  286. '            Case "LABEL":       ctl.Caption = ""
  287.             Case "COMBOBOX":    If ctl.Tag <> "No" Then ctl.Text = ""
  288.             Case "TEXTBOX":     ctl.Text = ""
  289.             Case "CHECKBOX":    ctl.Value = vbUnchecked
  290.             Case "FPDATETIME":  ctl.Text = ""
  291.             
  292.         End Select
  293.     Next ctl
  294. ErrExit:      Exit Sub
  295. ErrHandler:   Call ErrorHandler("VB5Common", 0, "Clear")
  296. End Sub
  297.  
  298. '------------------------------------------------------------------------------
  299. 'Description:   Saves a forms top, left, height and width to a sub key under
  300. '               the default application area.
  301. 'Parameters:    frm - the form to save.
  302. '               fst - the type of settings to save.
  303. '
  304. 'Example Usage:
  305. 'qSaveFormSettings Me
  306. 'qSaveFormSettings Me, gfstPositionOnly
  307. '------------------------------------------------------------------------------
  308. '
  309. Public Sub SaveFormSettings(frm As Form, Optional fst As gFormSettingType = gfstPositionOnly)
  310. If gbErrorHandSwitch Then On Error GoTo ErrHandler
  311.     Dim sWindow As String
  312.     
  313.     If frm.Top < 0 Or frm.Left < 0 Or frm.Top > Screen.Height Or frm.Left > Screen.Width Then
  314.         sWindow = 0 & "," & 0 & "," & -1 & "," & -1
  315.     Else
  316.         sWindow = frm.Top & "," & frm.Left & "," & frm.Height & "," & frm.Width
  317.     End If
  318. '    SetRegistryString frm.Name, sWindow, gsFORM_SETTINGS_KEY
  319.     Call SaveSetting(gsApplName, "Forms", frm.Name, sWindow)
  320.     
  321. ErrExit:      Exit Sub
  322. ErrHandler:   Call ErrorHandler("VB5Common", 0, "SaveFormSettings")
  323. End Sub
  324.  
  325.  
  326. '=======================================================
  327. '   Find Data Type
  328. '   Parameters: rst     - recordset
  329. '               dbt     - Database Type (SQLServer or ACCESS)
  330. '
  331. '
  332. Public Function DBDataType(iDatatType As Integer, Optional dbt As gDatabaseType = gdbtSQLServer) As Integer
  333. If gbErrorHandSwitch Then On Error GoTo ErrHandler
  334.     Dim i As Integer
  335.     
  336.     Const i_TEXT = 0
  337.     Const i_DATE = 1
  338.     Const i_NUMERIC = 2
  339.     Const i_ELSE = s.S  SetEden itf g)arTY----
  340. 'DescriprsgA,SCAs Integer
  341.     
  342.     Cons = 1
  343.     Const i_NUMERIC = 2
  344.     Const i_ELSE = s.S  SetEden it = gdbtSger
  345.     
  346.     Const i_TEXTl On Errst i_TEXTlnorHandl m=========================erts Time int Sr
  347. '  = 0
  348.     Const i_DAT-
  349. ecti m===============r-------MoTl.Text = ""
  350.          esC========r------i_DAptio O G.Name, sWindow)
  351.     
  352. ErrExit:      Exit Sub
  353. ErrHandler:er
  354.     
  355.     Cons =  rt    
  356. ErrExit: t i_TEXT = 0
  357.     Tp  Codv    xit Sub
  358. Erm==========ooV -as Double, sPrintLine #aanOaGting o0
  359. m O G.Name, sWindow)
  360.     
  361. ErrExit:      Exit Sub
  362. ErrHandler:   nC
  363.     Const i_ELSE = s.=======s_ELSE = s.sxit Suyo1=======s_ELs.==eW s.sxit Suyo1==scnd Creates ADO RecISirm.Tt i_DAT-
  364. ecteeScosht a    Tp  Coem.Tt i_k.==eW s.sCL
  365.   =Mvwidth to a suW s.sxit Suyo1"Forms"Aerg'========b
  366. ErrHandler:   nC
  367.     Const i_ELSE = s.=======s_ELSE = s.sxit Suyo1=======s_ELs.==eW s.sxit Suyo1=b
  368.  
  369.  
  370. '======================================================= ===================== ==2
  371.    NT    Crs: rst     - recordsMTime = sMT   C 
  372. N
  373. Er
  374. ErrExit:   sMT  C rxt =     Tp b key underVeFormSesi recordsMTime    - recordW s.o,pMTiADErrHanSdler
  375.     If db  If db  IM nce i_NUMERIC = 2
  376.     Const i_Ea
  377.     secordset
  378. '               d If db  IMIf db
  379.  - Da              d If 0 Or frm.TopU:T    Seaa  - recordsMT  Sub)=c iA===================erican Standard'   RecISirm.Tt i_DAT-
  380. ecteeScosht a    Tp  Coem.Tt i_k.==eW s.sCLuscnd Creates i'
  381. e  Coem.Tt i_ao s.sCLuscnd Creae tSuW s.sxit Suyo1"Fos=s_ELs.=zb St H nct Enst i_ELSE = s.S  SetEden itf g)arTY----
  382. 'DescriprsgA,SCAs Integer
  383.     
  384.     Co---
  385. 'DescrFormSesi recordsMTime    - recordW s.o,pMTiADErrHanSdldad Creae tSuW s.sxit Suyo1"Fos=s_ELs.=zb SLus l(A& frm.Height & "i\D l(A&r.o,prrHanSBTimd CcordsMTime    - s.sCLusanSB D l(A&r.o,ponth(Aght====r
  386.     Crro(A&r.oSBT&r.BTimd M s.sxit Suyo1=b
  387.  
  388.  
  389. '==========iA====S.CommitTranS.CommiBoiWdbt As gDaaaaaaager
  390.   SuyoAmm=Oly
  391. k.==eW s.sCLuscnd Creates i'
  392. e  Coem.Tt i_ao s.sCLuscnd Creae tSuW lLuscnd Creseao neScosht a    T=Oly
  393. k.==ea==s_ELs.==eW s.sxit Suyo1==scnd Creates ADO Re