Results 1 to 5 of 5

Thread: could any one explain this code for me ?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Arrow could any one explain this code for me ?

    Hi expert. could any one explain this code for me. I do not understand some parts of it. I know the output but not some parts of the code.Thanks



    Code:
    
    
    
    Private Sub processButton_Click()
    
      Dim fso       As New Scripting.FileSystemObject  <------
      Dim io        As Scripting.TextStream    <------
    
      
      Dim db        As DAO.Database
      Dim rst       As DAO.Recordset
      Dim fld       As DAO.Field
      '''Declreaing our variables
      Dim strBase   As String
      Dim strInsert As String
      Dim strFields As String
      Dim strValues As String
      Dim strTemp   As String
      Dim strFile   As String
      Dim strName   As String
      
      Set db = CurrentDb()
      
      Set rst = db.OpenRecordset(Me![ComboBox])
       
      strBase = "INSERT INTO " & Me![ComboBox] & "({%1}) VALUES ({%2})" <------
    
      strName = "c:\" & Me!ComboBox & " Data.sql"
    
      With rst  <------
    
        While Not .EOF
          strValues = ""  <------
    
          
                If Len(strFields) = 0 Then
            For Each fld In .Fields
              If Len(strFields) > 0 Then
          
                strFields = strFields & "," & fld.Name & ""
              Else
                '''strFields = "[" & fld.Name & "]"
                strFields = "" & fld.Name & ""
              End If
            Next fld
            strInsert = Replace(strBase, "{%1}", strFields)
          End If
    
               For Each fld In .Fields
            If Len(strValues) > 0 Then
              strValues = strValues & ","
            End If
            
            
            If IsNull(fld.Value) Then
            
           strValues = strValues & "null"
            Else
            v = fld.Value
            Select Case fld.Type
              Case dbMemo, dbText, dbChar
                strValues = strValues & "'" & v & "'"
              Case dbDate
                strValues = strValues & "#" & v & "#"
              Case Else
                strValues = strValues & v
            End Select
            
            End If
                  
            
            
            
          Next fld
          
              strTemp = Replace(strInsert, "{%2}", strValues)
          strFile = strFile & strTemp & vbNewLine
          
          .MoveNext
        Wend
        rst.Close
      End With
      
        If Len(strFile) > 0 Then
        Set io = fso.CreateTextFile(strName)
        io.Write strFile
        io.Close
      End If
      
    End Sub
    Last edited by tony007; Aug 11th, 2005 at 11:45 PM.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: could any one explain this code for me ?

    The first two lines are using the FileSystemObject which can be added as a reference to your project by locating the Microsoft Scripting Runtime Library.

    It looks like the INSERT code is taking the name of a table from a dropdown combo list, althought the %1 and %2 thing has me a bit baffled.

    The With statement is simply performing actions on a recordset object (rst)

    strValues = "" is emptying whatever was contained within the variable strValues.

  3. #3
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: could any one explain this code for me ?

    %1 and %2 are markers - they are replaced further down in the code.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  4. #4
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: could any one explain this code for me ?

    I see no need for the filesystemobject to be used here, a sequential file write will suffice therefore removing the need for the declaration on the FSO (It'll Speed the code up)

    VB Code:
    1. Dim fso As New Scripting.FileSystemObject
    2. Dim io As Scripting.TextStream
    3. 'other code
    4. '
    5. If Len(strFile) > 0 Then
    6.     Set io = fso.CreateTextFile(strName)
    7.     io.Write strFile
    8.     io.Close
    9. End If

    Replaces with

    VB Code:
    1. Dim FFile As Integer
    2. 'Other code
    3. '
    4. If Len(strFile) > 0 Then
    5.   FFile = FreeFile
    6.   Open strName For Output As #FFile
    7.   Print #FFile, strFile
    8.   Close #FFile
    9. End If
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: could any one explain this code for me ?

    Many thanks to all of you for your nice replies. I be happy if u explain to me the following:

    1)I wonder why we do like this :
    strFields = strFields & "," & fld.Name & ""
    rather then just puting fld.name equal to strFields.

    2)Does strInsert hold feild name only or any other part of sql statement ? what does replace do here?

    strInsert = Replace(strBase, "{%1}", strFields)


    3)what does strTemp holds ?

    strTemp = Replace(strInsert, "{%2}", strValues)

    4)Again why strFile after equal?

    strFile = strFile & strTemp & vbNewLine
    5) what does Wend doing ?

    6) why we check Len(strFields) = 0 and Len(strFields) > 0 ?
    Last edited by tony007; Aug 12th, 2005 at 03:54 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width