Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 11th, 2005, 11:49 PM
Friend of Wrox
 
Join Date: Mar 2005
Posts: 264
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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

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()

  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
 
Old August 13th, 2005, 02:02 AM
Friend of Wrox
 
Join Date: Jul 2005
Posts: 150
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Q#5: ends the With rst

TIP: most of your questions are answered in the help files. In the code window, select the VBA command and then press F1 for help.



Boyd
"Hi Tech Coach"
Access Based Accounting/Business Solutions developer.
http://www.officeprogramming.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Can someone explain this code to me pandu345 Java Basics 2 October 6th, 2006 12:25 PM
Explain some code on p178 jgrasso BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 1 June 3rd, 2005 09:51 PM
explain please rein Javascript 3 January 31st, 2005 09:47 AM
Please explain this code : p131 top para ababb BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 2 January 28th, 2005 06:02 PM
Could somebody explain this to me, please? czambran BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 3 June 7th, 2004 04:05 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.