home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 25: Programming / pc_actual_25.iso / Basic / GridOne / setup.EXE / SAMPLESLIB.BAS < prev    next >
Encoding:
BASIC Source File  |  2001-09-09  |  10.6 KB  |  349 lines

  1. Attribute VB_Name = "modSamplesLib"
  2. '-----------------------------------------------------------------------------
  3. ' This is a part of the BeeGrid ActiveX control.
  4. ' Copyright ⌐ 2000 Stinga
  5. ' All rights reserved.
  6. '
  7. ' You have a right to use and distribute the BeeGrid sample files in original
  8. ' form or modified, provided that you agree that Stinga has no warranty,
  9. ' obligations, or liability for any sample application files.
  10. '-----------------------------------------------------------------------------
  11. Option Explicit
  12.  
  13. Public g_NorthwindPath As String
  14.  
  15. Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
  16. Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
  17. Private Declare Function CommDlgExtendedError Lib "comdlg32.dll" () As Long
  18.  
  19. 'data buffer for the GetOpenFileName and GetSaveFileName functions
  20. Private Type OPENFILENAME
  21.         lStructSize As Long
  22.         hwndOwner As Long
  23.         hInstance As Long
  24.         lpstrFilter As String
  25.         lpstrCustomFilter As String
  26.         nMaxCustFilter As Long
  27.         iFilterIndex As Long
  28.         lpstrFile As String
  29.         nMaxFile As Long
  30.         lpstrFileTitle As String
  31.         nMaxFileTitle As Long
  32.         lpstrInitialDir As String
  33.         lpstrTitle As String
  34.         Flags As Long
  35.         nFileOffset As Integer
  36.         nFileExtension As Integer
  37.         lpstrDefExt As String
  38.         lCustData As Long
  39.         lpfnHook As Long
  40.         lpTemplateName As String
  41. End Type
  42.  
  43. Private Const PO_DLG_DELIMITER As String = ";"
  44. 'registry stuff
  45. Const HKEY_LOCAL_MACHINE = &H80000002
  46. Const KEY_QUERY_VALUE = &H1&
  47. Const KEY_ENUMERATE_SUB_KEYS = &H8&
  48. Const KEY_NOTIFY = &H10&
  49. Const READ_CONTROL = &H20000
  50. Const STANDARD_RIGHTS_READ = READ_CONTROL
  51. Const KEY_READ = STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY
  52.  
  53. Private Declare Function RegQueryValueEx& Lib "advapi32.dll" Alias "RegQueryValueExA" _
  54.    (ByVal hKey&, ByVal lpszValueName$, ByVal lpdwRes&, lpdwType&, ByVal lpDataBuff$, nSize&)
  55. Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" _
  56.    (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
  57. Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
  58.  
  59. Public Function OpenRecordset(sSQL$, _
  60.                               Optional CursorType As ADODB.CursorTypeEnum, _
  61.                               Optional LockType As ADODB.LockTypeEnum, _
  62.                               Optional Options As ADODB.CommandTypeEnum) As ADODB.Recordset
  63.    Dim rs As ADODB.Recordset
  64.    
  65.    If Len(g_NorthwindPath) = 0 Then
  66.       g_NorthwindPath = GetNorthwindPath()
  67.    End If
  68.    
  69.    Dim strCn$
  70.    strCn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & g_NorthwindPath & _
  71.            ";Persist Security Info=False"
  72.    
  73.    Dim conn As ADODB.Connection
  74.    Set conn = New ADODB.Connection
  75.    conn.ConnectionString = strCn
  76.    conn.CursorLocation = adUseClient
  77.    conn.Open
  78.    
  79.    Set rs = New Recordset
  80.    rs.Open sSQL, conn, CursorType, LockType, Options
  81.  
  82.    Set OpenRecordset = rs
  83. End Function
  84.  
  85. Private Function GetNorthwindPath() As String
  86.    
  87.    Dim sPath As String, sPathFile As String
  88.    
  89.    sPathFile = App.path & "\..\..\nwind.txt"
  90.       
  91.    If Len(sPath) = 0 Then
  92.       
  93.       ' Was db path saved before?
  94.       Dim fileNum As Integer
  95.       fileNum = FreeFile
  96.       On Error Resume Next
  97.       Open sPathFile For Input As fileNum
  98.       If Err.Number = 0 Then
  99.          Input #fileNum, sPath
  100.       End If
  101.       Close #fileNum
  102.       On Error GoTo 0
  103.       
  104.       If Len(sPath) > 0 Then
  105.          If Not FileDoesExist(sPath) Then sPath = ""
  106.       End If
  107.       
  108.       If Len(sPath) = 0 Then
  109.          ' Unable to locate northwind database.
  110.          ' Ask user to locate it
  111.          sPath = OpenNorthWind
  112.       End If
  113.       
  114.       ' Verify that database exists
  115.       If Len(sPath) > 0 Then
  116.          If FileDoesExist(sPath) Then
  117.             ' Write northwind's path to the persistent storage
  118.             fileNum = FreeFile
  119.             On Error Resume Next
  120.             Open sPathFile For Output As fileNum
  121.             If Err.Number = 0 Then
  122.                Print #fileNum, sPath
  123.             End If
  124.             Close #fileNum
  125.          Else
  126.             ' Northwind file does not exist
  127.             sPath = ""
  128.          End If
  129.       End If
  130.    End If
  131.    
  132.    GetNorthwindPath = sPath
  133. End Function
  134.  
  135.  
  136. Public Sub LoadDefinition( _
  137.    oGridObject As Object, sFileName As String)
  138.    Dim iFreeFile As Integer
  139.    Dim sDefinition As String
  140.    
  141.    sDefinition = String(FileLen(sFileName), " ")
  142.    
  143.    iFreeFile = FreeFile
  144.    
  145.    Open sFileName For _
  146.       Binary Access Read As iFreeFile
  147.    
  148.    Get #iFreeFile, , sDefinition
  149.    Close #iFreeFile
  150.    
  151.    oGridObject.SetDefinition sDefinition
  152. End Sub
  153.  
  154.  
  155. Public Sub SaveDefinition( _
  156.    oGridObject As Object, sFileName As String)
  157.    Dim iFreeFile As Integer
  158.    Dim sDefinition As String
  159.    
  160.    sDefinition = oGridObject.GetDefinition
  161.    
  162.    iFreeFile = FreeFile
  163.    
  164.    Open sFileName For _
  165.       Binary Access Write As iFreeFile
  166.    
  167.    Put #iFreeFile, , sDefinition
  168.    Close #iFreeFile
  169. End Sub
  170.  
  171.  
  172.  
  173. Private Function FileDoesExist(sFileName As String) As Boolean
  174.    
  175.    On Error GoTo FileExistError
  176.  
  177.    If (Len(VBA.Trim(sFileName)) = 0) Or (Len(Dir(sFileName)) = 0) Then
  178.       FileDoesExist = False
  179.    Else
  180.       FileDoesExist = True
  181.    End If
  182.    
  183.    Exit Function
  184. FileExistError:
  185.    FileDoesExist = False
  186.    Exit Function
  187. End Function
  188. Private Function GetProfileString(sSection$, sEntry$) As String
  189.    Dim lSecKey As Long, sTmp As String * 255
  190.    Dim lResult As Long, lLen As Long
  191.    
  192.    lLen = 255
  193.    
  194.    lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, sSection, 0, KEY_READ, lSecKey)
  195.    If lResult = 0 Then
  196.       lResult = RegQueryValueEx(lSecKey, sEntry, 0, 0, sTmp, lLen)
  197.       If lResult = 0 Then
  198.          GetProfileString = Left$(sTmp, lLen - 1)
  199.       End If
  200.       RegCloseKey lSecKey
  201.    End If
  202.     
  203. End Function
  204.  
  205. Private Function GetVBPath() As String
  206.    Dim sPath As String
  207.    Const VB5_REG_SECTION = "SOFTWARE\Microsoft\Visual Basic\5.0"
  208.    Const VB5_REG_ENTRY = "InstallDir"
  209.    Const VB6_REG_SECTION = "Software\Microsoft\VisualStudio\6.0\Setup\Microsoft Visual Basic"
  210.    Const VB6_REG_ENTRY = "ProductDir"
  211.    
  212.    sPath = GetProfileString(VB6_REG_SECTION, VB6_REG_ENTRY)
  213.    
  214.    If Len(sPath) = 0 Then _
  215.       sPath = GetProfileString(VB5_REG_SECTION, VB5_REG_ENTRY)
  216.  
  217.     GetVBPath = sPath
  218. End Function
  219.  
  220. Public Function OpenNorthWind() As String
  221.    Dim sVBPath As String
  222.    Dim sNorthWind As String
  223.  
  224.    sVBPath = GetVBPath
  225.    
  226.    If Len(sVBPath) > 0 Then
  227.       sNorthWind = sVBPath & "\Nwind.mdb"
  228.       If FileDoesExist(sNorthWind) Then
  229.          OpenNorthWind = sNorthWind
  230.          Exit Function
  231.       End If
  232.    End If
  233.    
  234.    OpenNorthWind = ShowOpen( _
  235.       "MS Access Database *.mdb|*.mdb|All Files *.*|*.*", _
  236.       "Open NortWind Database")
  237.       
  238. End Function
  239.  
  240. Public Function ShowOpen( _
  241.    Optional sFilter As String = "All Files *.*|*.*", _
  242.    Optional sTitle As String = "Open file") As String
  243.    'display the file dialog for ShowOpen or ShowSave
  244.    Const DLG_ACTION = 1
  245.    Const dlg_poPathMustExist = &H800
  246.    Const dlg_poFileMustExist = &H1000
  247.    Const dlg_poExplorer = &H80000
  248.    Const dlg_Flags = dlg_poPathMustExist + dlg_poFileMustExist + dlg_poExplorer
  249.    Const dlg_MaxSize = 255
  250.    Const dlg_MaxFileSize = 1024
  251.    Dim tOpenFile As OPENFILENAME
  252.    Dim sFileNameBuff As String, sFileTitleBuff As String
  253.    Dim sValue As String, lApiReturn As Long
  254.    Dim lExtendedError As Long, n As Long
  255.    Dim buf As String, dirBuf As String, sSep As String
  256.    
  257.    On Error GoTo ShowFileDialogError
  258.    
  259.    'init property buffers
  260.    lApiReturn = 0  'APIReturn property
  261.    lExtendedError = 0  'ExtendedError property
  262.        
  263.    'prepare tOpenFile data
  264.    tOpenFile.lStructSize = Len(tOpenFile)
  265.    tOpenFile.hwndOwner = 0
  266.    tOpenFile.lpstrFilter = APIFilter(sFilter)
  267.    tOpenFile.iFilterIndex = 0
  268.    
  269.    'tOpenFile.lpstrFile As Long - init from FileName property
  270.    'prepare sFileNameBuff
  271.    sFileNameBuff = ""
  272.    sFileNameBuff = Space(dlg_MaxSize - 1)
  273.    'trim to length of dlg_MaxFileSize - 1
  274.    sFileNameBuff = Mid$(sFileNameBuff, 1, dlg_MaxFileSize - 1)
  275.    'null terminate
  276.    sFileNameBuff = sFileNameBuff & Chr$(0)
  277.    tOpenFile.lpstrFile = sFileNameBuff
  278.     
  279.    'nMaxFile As Long - init from MaxFileSize property
  280.    tOpenFile.nMaxFile = dlg_MaxFileSize
  281.             
  282.    'lpstrFileTitle As String - init from FileTitle property
  283.    'prepare sFileTitleBuff
  284.    sFileTitleBuff = sTitle
  285.    'pad with spaces
  286.    sFileTitleBuff = Space(dlg_MaxSize - 1)
  287.    'trim to length of dlg_MaxFileSize - 1
  288.    sFileTitleBuff = Mid$(sFileTitleBuff, 1, dlg_MaxFileSize - 1)
  289.    'null terminate
  290.    sFileTitleBuff = sFileTitleBuff & Chr$(0)
  291.    tOpenFile.lpstrFileTitle = sFileTitleBuff
  292.         
  293.    'tOpenFile.lpstrInitialDir As String - init from InitDir property
  294.    tOpenFile.lpstrInitialDir = App.path
  295.    
  296.    'tOpenFile.lpstrTitle As String - init from DialogTitle property
  297.    tOpenFile.lpstrTitle = "Open File"
  298.    
  299.    tOpenFile.Flags = dlg_Flags
  300.       
  301.    'tOpenFile.lpstrDefExt As String - init from DefaultExt property
  302.    tOpenFile.lpstrDefExt = ""
  303.    'call the GetOpenFileName API function
  304.    lApiReturn = GetOpenFileName(tOpenFile)
  305.     
  306.     'handle return from GetOpenFileName API function
  307.     Select Case lApiReturn
  308.         Case 0  'user canceled
  309.             'generate an error
  310.             Err.Raise (2001)
  311.             Exit Function
  312.         Case 1  'user selected or entered a file
  313.             'sFileName gets part of tOpenFile.lpstrFile to the left of first Chr$(0)
  314.             ShowOpen = Left$(tOpenFile.lpstrFile, InStr(tOpenFile.lpstrFile, Chr$(0)) - 1)
  315.         Case Else   'an error occured
  316.             lExtendedError = CommDlgExtendedError
  317.     End Select
  318.  
  319. Exit Function
  320.  
  321. ShowFileDialogError:
  322.     Exit Function
  323. End Function
  324.  
  325.  
  326. Private Function APIFilter(sIn)
  327.     'prepares sIn for use as a filter string in API common dialog functions
  328.     Dim lChrNdx As Long
  329.     Dim sOneChr As String
  330.     Dim sOutStr As String
  331.     
  332.     'convert any | characters to nulls
  333.     For lChrNdx = 1 To Len(sIn)
  334.         sOneChr = Mid$(sIn, lChrNdx, 1)
  335.         If sOneChr = "|" Then
  336.             sOutStr = sOutStr & Chr$(0)
  337.         Else
  338.             sOutStr = sOutStr & sOneChr
  339.         End If
  340.     Next
  341.     
  342.     'add a null to the end
  343.     sOutStr = sOutStr & Chr$(0)
  344.     
  345.     'return sOutStr
  346.     APIFilter = sOutStr
  347.     
  348. End Function
  349.