could any one explain this code for me ?

Joined
Jun 30, 2005
Messages
59
Reaction score
0
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

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 ?
Code:
Private Sub processButton_Click()
[b]
  Dim fso       As New Scripting.FileSystemObject  <------
  Dim io        As Scripting.TextStream    <------
[/b]
  
  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])
   
  [b]strBase = "INSERT INTO " & Me![ComboBox] & "({%1}) VALUES ({%2})" <------[/b]

  strName = "c:\" & Me!ComboBox & " Data.sql"

 [b] With rst  <------[/b]

    While Not .EOF
    [b]  strValues = ""  <------[/b]

     [b] 
            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
  [/b]
    If Len(strFile) > 0 Then
    Set io = fso.CreateTextFile(strName)
    io.Write strFile
    io.Close
  End If
  
End Sub
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top