lalachka
lalachka

Reputation: 413

copy field value to text file and save as .sql

in MS Access i have a field that stores the SQL used for a project. the sql field is a memo field with "Rich Text" property enabled. what that means is that a code below

CREATE TABLE NR_TMP_1_104 AS
     SELECT DISTINCT tmp.otherid
                    ,tmp.groupno
       FROM NR_tmp_1_900 tmp
           ,NR_TMP_1_103 vp
      WHERE tmp.otherid = vp.otherid;



      SELECT groupno, COUNT(otherid) Counts
      FROM NR_TMP_1_104
      GROUP BY groupno;

     SELECT otherid, actualvpchoice FROM NR_TMP_2_102

really looks like all the way below (i'm sorry for this huge chunk). so that means i can't do docmd.transfertext, the only way to get normal looking text is to copy and paste.

i need to be able to export this field to a text file and save it with the extension of .sql.

here's what i have so far))))

Forms!freports!sql.SetFocus
DoCmd.RunCommand acCmdCopy

how do i open a text file, paste what's on my clipboard and save it as .sql.

thank you very much

"<div>&nbsp;</div>

<div><font size=3 color=blue style=""BACKGROUND-COLOR:#FFFFFF"">CREATE</font><font
size=3 color=black style=""BACKGROUND-COLOR:#FFFFFF""> </font><font size=3
color=blue style=""BACKGROUND-COLOR:#FFFFFF"">TABLE</font><font size=3
color=black style=""BACKGROUND-COLOR:#FFFFFF""> </font><font size=3 color=olive
style=""BACKGROUND-COLOR:#FFFFFF"">NR_TMP_1_104</font><font size=3 color=black
style=""BACKGROUND-COLOR:#FFFFFF""> </font><font size=3 color=blue
style=""BACKGROUND-COLOR:#FFFFFF"">AS</font></div>

<div><font size=3 color=black style=""BACKGROUND-COLOR:#FFFFFF"">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font
size=3 color=blue style=""BACKGROUND-COLOR:#FFFFFF"">SELECT</font><font size=3
color=black style=""BACKGROUND-COLOR:#FFFFFF""> </font><font size=3 color=blue
style=""BACKGROUND-COLOR:#FFFFFF"">DISTINCT</font><font size=3 color=black
style=""BACKGROUND-COLOR:#FFFFFF""> tmp</font><font size=3 color=blue
style=""BACKGROUND-COLOR:#FFFFFF"">.</font><font size=3 color=black
style=""BACKGROUND-COLOR:#FFFFFF"">otherid</font></div>

<div><font size=3 color=black style=""BACKGROUND-COLOR:#FFFFFF"">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font
size=3 color=blue style=""BACKGROUND-COLOR:#FFFFFF"">,</font><font size=3
color=black style=""BACKGROUND-COLOR:#FFFFFF"">tmp</font><font size=3 color=blue
style=""BACKGROUND-COLOR:#FFFFFF"">.</font><font size=3 color=black
style=""BACKGROUND-COLOR:#FFFFFF"">groupno</font></div>

<div><font size=3 color=black style=""BACKGROUND-COLOR:#FFFFFF"">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font
size=3 color=blue style=""BACKGROUND-COLOR:#FFFFFF"">FROM</font><font size=3
color=black style=""BACKGROUND-COLOR:#FFFFFF""> NR_tmp_1_900 tmp</font></div>

<div><font size=3 color=black style=""BACKGROUND-COLOR:#FFFFFF"">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font
size=3 color=blue style=""BACKGROUND-COLOR:#FFFFFF"">,</font><font size=3
color=olive style=""BACKGROUND-COLOR:#FFFFFF"">NR_TMP_1_103</font><font size=3
color=black style=""BACKGROUND-COLOR:#FFFFFF""> vp</font></div>

<div><font size=3 color=black style=""BACKGROUND-COLOR:#FFFFFF"">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font
size=3 color=blue style=""BACKGROUND-COLOR:#FFFFFF"">WHERE</font><font size=3
color=black style=""BACKGROUND-COLOR:#FFFFFF""> tmp</font><font size=3
color=blue style=""BACKGROUND-COLOR:#FFFFFF"">.</font><font size=3 color=black
style=""BACKGROUND-COLOR:#FFFFFF"">otherid </font><font size=3 color=blue
style=""BACKGROUND-COLOR:#FFFFFF"">=</font><font size=3 color=black
style=""BACKGROUND-COLOR:#FFFFFF""> vp</font><font size=3 color=blue
style=""BACKGROUND-COLOR:#FFFFFF"">.</font><font size=3 color=black
style=""BACKGROUND-COLOR:#FFFFFF"">otherid</font><font size=3 color=blue
style=""BACKGROUND-COLOR:#FFFFFF"">;</font></div>

<div><font size=3 color=black style=""BACKGROUND-COLOR:#FFFFFF"">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font></div>

<div><font size=3 color=black style=""BACKGROUND-COLOR:#FFFFFF"">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font></div>

<div><font size=3 color=black style=""BACKGROUND-COLOR:#FFFFFF"">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font></div>

<div><font size=3 color=black style=""BACKGROUND-COLOR:#FFFFFF"">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font
size=3 color=blue style=""BACKGROUND-COLOR:#FFFFFF"">SELECT</font><font size=3
color=black style=""BACKGROUND-COLOR:#FFFFFF""> groupno</font><font size=3
color=blue style=""BACKGROUND-COLOR:#FFFFFF"">,</font><font size=3 color=black
style=""BACKGROUND-COLOR:#FFFFFF""> </font><font size=3 color=blue
style=""BACKGROUND-COLOR:#FFFFFF"">COUNT(</font><font size=3 color=black
style=""BACKGROUND-COLOR:#FFFFFF"">otherid</font><font size=3 color=blue
style=""BACKGROUND-COLOR:#FFFFFF"">)</font><font size=3 color=black
style=""BACKGROUND-COLOR:#FFFFFF""> Counts</font></div>

<div><font size=3 color=black style=""BACKGROUND-COLOR:#FFFFFF"">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font
size=3 color=blue style=""BACKGROUND-COLOR:#FFFFFF"">FROM</font><font size=3
color=black style=""BACKGROUND-COLOR:#FFFFFF""> </font><font size=3 color=olive
style=""BACKGROUND-COLOR:#FFFFFF"">NR_TMP_1_104</font></div>

<div><font size=3 color=black style=""BACKGROUND-COLOR:#FFFFFF"">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font
size=3 color=blue style=""BACKGROUND-COLOR:#FFFFFF"">GROUP</font><font size=3
color=black style=""BACKGROUND-COLOR:#FFFFFF""> </font><font size=3 color=blue
style=""BACKGROUND-COLOR:#FFFFFF"">BY</font><font size=3 color=black
style=""BACKGROUND-COLOR:#FFFFFF""> groupno</font><font size=3 color=blue
style=""BACKGROUND-COLOR:#FFFFFF"">;</font></div>

<div><font size=3 color=black style=""BACKGROUND-COLOR:#FFFFFF"">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font></div>

<div><font size=3 color=black style=""BACKGROUND-COLOR:#FFFFFF"">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font
size=3 color=blue style=""BACKGROUND-COLOR:#FFFFFF"">SELECT</font><font size=3
color=black style=""BACKGROUND-COLOR:#FFFFFF""> otherid</font><font size=3
color=blue style=""BACKGROUND-COLOR:#FFFFFF"">,</font><font size=3 color=black
style=""BACKGROUND-COLOR:#FFFFFF""> actualvpchoice </font><font size=3
color=blue style=""BACKGROUND-COLOR:#FFFFFF"">FROM</font><font size=3
color=black style=""BACKGROUND-COLOR:#FFFFFF""> NR_TMP_2_102</font></div>"

Upvotes: 2

Views: 1066

Answers (2)

lalachka
lalachka

Reputation: 413

just for reference, if anyone ever needs it. HansUp's code works fine and doesn't use any references (the code below needs MS Forms reference) and is just overall a much better coding example.

however, if anyone needs the quotes removed then here's one way of doing it.

Dim objFSO
Dim objFile
Dim ClipboardText

Me.sql.SetFocus
DoCmd.RunCommand acCmdCopy


Dim DataObj As New MSForms.DataObject

 DataObj.GetFromClipboard
 ClipboardText = DataObj.GetText



Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\mysql.sql", 2, True)
objFile.WriteLine ClipboardText
objFile.Close

Upvotes: 1

HansUp
HansUp

Reputation: 97101

You can use the PlainText() function to get just the plain text from your memo field's rich text. Here's an example from the Immediate window.

? DLookup("memo_field", "tblFoo", "id=1")
<div>a <strong>b </strong>c</div>

? PlainText(DLookup("memo_field", "tblFoo", "id=1"))
a b c

Once you get the plain text, you don't need to go through Notepad in order to write it to a file. You can write to a file directly using VBA file functions or via the Scripting.FileSystemObject methods. Here is an example of the first approach. (I'm assuming Forms!freports!sql contains the rich text.)

Dim FileNum As Integer
FileNum = FreeFile()
Open "C:\SomeFolder\your.sql" For Output As #FileNum
Write #FileNum, PlainText(Forms!freports!sql)
Close #FileNum

Upvotes: 2

Related Questions