home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.visual.basic      MS Visual Basic discussions, NOT dot-net      10,840 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 8,888 of 10,840   
   Calan to All   
   Re: New, Open, Save, Save As, Close (1/2   
   16 Oct 04 00:54:25   
   
   XPost: comp.lang.basic.visual   
   From: none@nospam.com   
      
   Andras,   
      
   One technique that I've used in the past is to use an Access database (coded   
   with ADO in VB), and then put the name of each field in it's corresponding   
   input control's Tag property. You can then use a For Each Control type of   
   loop to easily get the control's value and store it to the field named in   
   it's tag property. The same process works in reverse to load a previously   
   saved group of input values.   
      
   To take this a step further, if you have combo boxes with a list of values   
   to select from, you can write a generic routine to load the lists and then   
   use a text file to store all the values for each listbox. This makes adding   
   or removing an item from a listbox a simple matter of editing the text file.   
   Again, use the tag property to correlate the control with it's list of   
   values in the file.   
      
   Here are some routines I wrote a few years ago to do this. These are copied   
   straight from my projects, so you'll need to tweak them, but you'll get the   
   idea. (This project stored engineering design data, so records are loaded   
   and stored based on a "Design ID" field. It's just an Access AutoNumber   
   field.   
      
   (InputDatabase is a global variable that points to the Access database)   
      
   *****************   
      
   Public Sub LoadDesignFromDB(Activeform As Form, DesignID As Long)   
      
      ' This routine loads the contents of all controls on the main form that   
   contain a tag value   
      ' from the selected record in the design database   
      
      Dim CurrentControl As Control   
      Dim f As ADODB.Field   
      Dim Msg As String   
      Dim dbInputDatabase As New ADODB.Connection   
      Dim rs As New ADODB.Recordset   
      Dim SQLStatement As String   
      
      On Error GoTo ErrHandler   
      
      SQLStatement = "SELECT * FROM InputData WHERE DesignID = " & DesignID   
      
      Screen.MousePointer = vbHourglass   
      
      dbInputDatabase.Open (ProviderString & ";Data Source=" & InputDatabase)   
      rs.Open SQLStatement, dbInputDatabase, adOpenKeyset, adLockOptimistic   
      
      For Each f In rs.Fields   
         For Each CurrentControl In Activeform.Controls   
            If CurrentControl.Tag = f.Name Then   
               If f.Type = adBoolean Then   
                  CurrentControl = Abs(f.Value)   
               Else   
                  CurrentControl = f.Value   
               End If   
            End If   
         Next   
      Next   
      
      rs.Close   
      dbInputDatabase.Close   
      Set rs = Nothing   
      Set dbInputDatabase = Nothing   
      
      Screen.MousePointer = vbDefault   
      
      MsgBox "Design data loaded sucessfully", vbOKOnly + vbInformation, "Load   
   Design Data"   
      
      Exit Sub   
      
   ErrHandler:   
      
      Screen.MousePointer = vbDefault   
      Select Case Err   
         Case Else   
            Msg = "Unable to load design data. Error Number: " & Err.Number &   
   vbCrLf & Err.Description   
      End Select   
      MsgBox Msg, vbCritical + vbOKOnly, "Load Design Data"   
      
      On Error Resume Next   
      
      rs.Close   
      dbInputDatabase.Close   
      Set rs = Nothing   
      Set dbInputDatabase = Nothing   
      
   End Sub   
      
   *****************   
      
   Public Sub SaveDesignToDB(Activeform As Form, SaveMode As String)   
      
      ' This routine saves the contents of all controls on the form that   
   contain a tag value   
      ' to a the Design database   
      
      Dim Msg As String   
      Dim Style As Integer   
      Dim Title As String   
      Dim Response As Integer   
      
      Dim CurrentControl As Control   
      Dim ControlTag As String   
      
      Dim dbInputDatabase As New ADODB.Connection   
      Dim rs As New ADODB.Recordset   
      
      Dim SQLStatement As String   
      
      On Error GoTo ErrHandler   
      
      ' If DesignID has been reset, save as new; otherwise, update current   
   record   
      If SaveMode = "NEW" Then   
         Msg = "Save current data as new design in database?"   
         SQLStatement = "InputData"   
      Else   
         Msg = "Overwrite current design in database?"   
         SQLStatement = "SELECT * FROM InputData WHERE DesignID = " &   
   CurrentDesignID & ""   
      End If   
      
      Style = vbQuestion + vbOKCancel '+ vbDefaultButton2   
      Title = "Save Design Data"   
      Response = MsgBox(Msg, Style, Title)   
      
      If Response <> vbOK Then   
         Exit Sub   
      End If   
      
      Screen.MousePointer = vbHourglass   
      
      dbInputDatabase.Open (ProviderString & ";Data Source=" & InputDatabase)   
      rs.Open SQLStatement, dbInputDatabase, adOpenKeyset, adLockOptimistic   
      
      If SaveMode = "NEW" Then   
         rs.AddNew   
      End If   
      
      For Each CurrentControl In Activeform.Controls   
         ControlTag = CurrentControl.Tag   
         If ControlTag <> "" Then   
            rs.Fields(ControlTag) = CurrentControl   
         End If   
      Next   
      rs.Update   
      rs.MoveFirst   
      
      MsgBox "Design data saved successfully.", vbOKOnly, "Save Design Data"   
      
      rs.Close   
      dbInputDatabase.Close   
      Set rs = Nothing   
      Set dbInputDatabase = Nothing   
      
      Screen.MousePointer = vbDefault   
      
      Exit Sub   
      
   ErrHandler:   
      
      Screen.MousePointer = vbDefault   
      Select Case Err   
         Case Else   
            Msg = "Unable to save design data. Error Number: " & Err.Number &   
   vbCrLf & Err.Description   
      End Select   
      MsgBox Msg, vbCritical + vbOKOnly, "Save Design Data"   
      
      On Error Resume Next   
      
      rs.Close   
      dbInputDatabase.Close   
      Set rs = Nothing   
      Set dbInputDatabase = Nothing   
      
   End Sub   
      
   *****************   
      
   Public Sub LoadLists(Activeform As Form, ListFileName As String)   
      
      ' This routine loads each combo box from the "ListData.dat" file   
      ' in the App.Path\config directory. This is a very fast, easy way   
      ' of maintaining combo lists.   
      
      ' This function works by searching the main form for a control with an   
      ' assigned tag property that matches a tag entered in the listdata file.   
      ' If one is found, it's list is populated with the values following the   
   tag   
      ' entry.   
      
      Dim FileHandle As Integer   
      Dim FileName As String   
      Dim TextInput As String   
      Dim ItemToAdd As String   
      Dim Counter As Integer   
      Dim Msg As String   
      
      Dim ControlTag As String   
      Dim DefaultListIndex As Integer   
      Dim CurrentControl As Control   
      
      On Error GoTo ErrHandler   
      
      FileHandle = FreeFile   
      FileName = App.Path & "\config\ListData.dat"   
      
      ' Load lists for combo boxes   
      Open FileName For Input As #1   
         Do Until EOF(FileHandle)   
            Line Input #FileHandle, TextInput   
            If UCase(TextInput) = "CONTROL" Then   
               ItemToAdd = ""   
               Input #FileHandle, ControlTag, DefaultListIndex   
               For Each CurrentControl In Activeform.Controls   
                  If CurrentControl.Tag = ControlTag Then   
                     Do Until UCase(ItemToAdd) = "EOL"   
                        Line Input #FileHandle, ItemToAdd   
                        If UCase(ItemToAdd) = "EOL" Then Exit Do   
                        CurrentControl.AddItem ItemToAdd   
                     Loop   
                     CurrentControl.ListIndex = DefaultListIndex   
                     Exit For   
                  End If   
               Next   
            End If   
         Loop   
      Close #FileHandle   
      
      Exit Sub   
      
   ErrHandler:   
      
      Close #FileHandle   
      Screen.MousePointer = vbDefault   
      Select Case Err   
         Case Else   
            Msg = "Error in ListData.dat file. " & Err.Description & "."   
      
   [continued in next message]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca