MacBook, defective by design banner

title:
Put the knife down and take a green herb, dude.


descrip:

One feller's views on the state of everyday computer science & its application (and now, OTHER STUFF) who isn't rich enough to shell out for www.myfreakinfirst-andlast-name.com

Using 89% of the same design the blog had in 2001.

FOR ENTERTAINMENT PURPOSES ONLY!!!
Back-up your data and, when you bike, always wear white.

As an Amazon Associate, I earn from qualifying purchases. Affiliate links in green.

x

MarkUpDown is the best Markdown editor for professionals on Windows 10.

It includes two-pane live preview, in-app uploads to imgur for image hosting, and MultiMarkdown table support.

Features you won't find anywhere else include...

You've wasted more than $15 of your time looking for a great Markdown editor.

Stop looking. MarkUpDown is the app you're looking for.

Learn more or head over to the 'Store now!

Thursday, August 09, 2012

Okay, maybe vba-ing your way from Excel files to raw SQL isn't the way most folks extract, transform, and load, but I like it okay when you get an Excel file of values.

This code is essentially the same as this jive from 2009, but I like that I've finally given up on adding a UserForm, naming it, adding a text box, naming it too, then stretching each until they look like a finished product.

Instead, I now just add a form, double-click a TextBox on there, and let this programmatically do the rest.

Option Explicit

Public Sub createSql()
Dim i As Integer
Dim intStart As Integer
Dim intEnd As Integer

Dim strInsertSql As String
Dim strUpdateSql As String
Dim strLine As String

Dim strOut As String


intStart = 2
intEnd = 963

i = intStart
While (Not "" = Cells(i, 1).Value) ' assuming there are no blanks in the first col
'While (i <= intEnd) ' if you'd rather hard-code


strLine = "INSERT INTO TABLE1 " & _
" (FIELD1, FIELD2) " & _
" VALUES " & _
" (" & _
CStr(Cells(i, 1).Value) & "," & _
CStr(Cells(i, 2).Value) & _
")"

strInsertSql = strInsertSql & strLine & "; -- " & (i - intStart + 1) & vbNewLine



strLine = "UPDATE TABLE2 SET " & _
"FIELD1 = '" & CStr(Cells(i, 1).Value) & "' " & _
"WHERE FIELD3 = '" & CStr(Cells(i, 2).Value) & "'; -- " & (i - intStart + 1)

strUpdateSql = strUpdateSql & strLine & "; -- " & (i - intStart + 1) & vbNewLine



i = i + 1
Wend


strOut = strInsertSql & vbNewLine & _
vbNewLine & _
"--------------------------------------------------" & vbNewLine & _
vbNewLine & _
strUpdateSql & vbNewLine & _
vbNewLine & _
"--------------------------------------------------" & vbNewLine & _
"--------------------------------------------------" & vbNewLine & _
vbNewLine



'============================================
' making this so I don't have to set up the
' danged userform and textbox any more
'============================================
UserForm1.Height = 800
UserForm1.Width = 1000
With UserForm1.TextBox1
.Top = 10
.Left = 10
.Height = UserForm1.Height - 40
.Width = UserForm1.Width - 20
.MultiLine = True
.ScrollBars = fmScrollBarsBoth
End With
'============================================
'============================================


UserForm1.TextBox1.Text = strOut
UserForm1.Show

End Sub

Labels: , , ,


posted by ruffin at 8/09/2012 01:13:00 PM
Tuesday, April 19, 2011

This stinks. Really. Say what you will about Microsoft, they know how to create and maintain a programming language. Whoever thought that s/vbscript/Applescript/gi would be a fair substitution in Office for Mac (even ignoring that it breaks legacy macros) was not a programmer.

Here's what my "final" code looks like. It's a mess and needs cleaning. But something that's INSANELY EASY in vbscript takes the below to do something similar. I couldn't even get it to know what cell format selections I'd made in Excel and ended up with an ugly kludge for zip codes and a hellah ugly kludge for phone numbers which were coming out in a format like "9.195553662E+9". What a mess.

I could clean it up some, taking out lesser kludges that aren't working, but I think you get the point better as is. Not intuitive. Not fully-featured. The stuff you have to roll yourself in Applescript (afaict) is insane and inane. Boy. Talk about not feeling fully featured.

Now I have to let it wade through over 3000 lines of Excel tonight...

   1 on replaceText(find, replace, subject)
2 set prevTIDs to text item delimiters of AppleScript
3 set text item delimiters of AppleScript to find
4 set subject to text items of subject
5
6 set text item delimiters of AppleScript to replace
7 set subject to "" & subject
8 set text item delimiters of AppleScript to prevTIDs
9
10 return subject
11 end replaceText
12
13 tell application "Microsoft Excel"
14
15 -- from http://lists.apple.com/archives/applescript-users/2009/Jun/msg00331.html
16 set hc to find (row 1 of used range of active sheet) what "PHONE"
17 set n to first column index of hc
18 set f to first column index of (column 1 of used range of active sheet)
19 set col to column (n - f + 1) of used range of active sheet
20 set number format of col to "0"
21 -- doesn't seem to work
22
23
24 -- say "Saving with every row"
25
26 set lastCol to 10 -- yes, I'm cheating.
27 -- you have to know the last col and row for this to work
28 set lastRow to 100 -- I suppose I could add dialog boxes to ask max nums
29 set c to 1
30 set r to 90 -- that's the first row that's got info it in; skip the first row's col headers
31 set strCmd to ""
32
33 try
34 repeat until r = lastRow + 1
35 set strCmd to strCmd & "INSERT INTO restaurants (phone, company, address, ¬
36 city, state, zip, owner, gm, agent, chef)"
37 set strCmd to strCmd & " VALUES ("
38 set daComma to ""
39
40 --'','','',42,,'','','')"
41
42 set c to 1
43
44 repeat until c = lastCol + 1
45 --display dialog "Row: " & r & " Col: " & c
46
47 set theValue to get value of cell c of row r of active sheet
48
49 if (c = 1) then -- then we have a phone number
50 -- Excel via Applescript is borking numbers like mad.
51 -- There are Right Ways to handle this. They
52 -- don't see to be working well (see above).
53 -- we're going to cheat like mad instead.
54 -- TODO: KLUDGE-A-RIFFIC
55 if (text 11 of (theValue as string) = "E") then
56 set strOut to text 1 of (theValue as string) ¬
57 & text 3 thru 10 of (theValue as string) & "0"
58 else
59 set strOut to text 1 of (theValue as string) ¬
60 & text 3 thru 11 of (theValue as string)
61 end if
62
63 set strCmd to strCmd & daComma & strOut
64 set daComma to ","
65
66 else if (c = 6) then -- then we have a zip code
67 -- can't use this branch for phone numbers
68 -- as they evaluate to a number larger than
69 -- our largest possible integer
70 if (count of characters of (theValue as string)) = 5 then
71 set theValue to theValue as integer
72 end if
73 set strCmd to strCmd & daComma & (theValue as string)
74 set daComma to ","
75 else if (c = 5) then
76 set strCmd to strCmd & daComma & "42"
77 -- hard coding SC to save a lookup
78 set daComma to ","
79 else -- else it's a string
80 if theValue = "" then
81 set theValue to "empty"
82 end if
83 set strCmd to strCmd & daComma & "'" & (theValue as string) & "'"
84 set daComma to ","
85 end if
86
87 set c to (c + 1)
88 end repeat
89 set strCmd to strCmd & ")" & return -- return adds a MacOS newline
90 set r to (r + 1)
91 if r mod 50 = 0 then
92 -- beep -- add some sort of feedback.
93 say r -- add cooler feedback. This talks.
94 -- Btw, Applescript for this in Excel is insanely slooooow
95 -- vs. my experience with vbs. It takes several seconds to
96 -- do 10 rows of ten columns of customer address and phone, etc.
97 -- information.
98 end if
99 end repeat
100
101 set theFilePath to (path to desktop as string) & "test11.txt" as string
102 set theFileReference to open for access theFilePath with write permission
103 write strCmd to theFileReference
104 close access theFileReference
105
106 on error errMessage number errNum from errSource partial result errList ¬
107 to class_constant -- various variables store information about the error
108 set bigmessage to "The error is: " & errMessage & return ¬
109 & "The number is: " & errNum & return & "The source is: " & errSource & return ¬
110 & "r&c: " & r & "&" & c
111 error bigmessage -- error statement displays dialog box to user
112 say "error in the loop"
113 close access theFileReference
114 -- this close seems important. I can't tell how to get
115 -- back the handle to close this if I had an error cause it
116 -- to stay open from a past script invocation.
117 end try
118
119 say "Done!"
120 -- display dialog "Done with write to file"
121 end tell


(Edit: See this on tells. I had to tweak the above a good bit for it to work well, but it still serves as a pretty decent low-investment Applescript (vs. VBA) code snippet. It's an order of magnitude more complicated.)

Labels: , , , ,


posted by ruffin at 4/19/2011 04:07:00 PM
Sunday, August 30, 2009

Update 20100216: Now it even works!

' from http://support.microsoft.com/kb/188007
' for running on Excel on Mac, where the VBA engine doesn't
' have VB6 String functions
Public Function Replace(sIn As String, sFind As String, _
sReplaceWith As String, Optional nStart As Long = 1, _
Optional lngMaxReplacementsToMake As Long = -1) As String

Dim lngNumReplacementsDeja As Long
Dim nPos As Integer
Dim sOut As String
Dim bContinue As Boolean

bContinue = True

sOut = sIn
nPos = InStr(nStart, sOut, sFind, vbBinaryCompare)
While (nPos > 0) And bContinue
lngNumReplacementsDeja = lngNumReplacementsDeja + 1
sOut = Left(sOut, nPos - 1) &_
sReplaceWith & Mid(sOut, nPos + Len(sFind))
nPos = nPos + Len(sReplaceWith)

If (lngMaxReplacementsToMake <> -1) And _
(lngNumReplacementsDeja >= lngMaxReplacementsToMake) Then

bContinue = False
End If

nPos = InStr(nPos, sOut, sFind, vbBinaryCompare)

If nPos > 10000 Then
' I put a breakpoint here when testing
Debug.Print nPos & " :: " & sOut & " :: " & sFind
End If
Wend

EndFn:
Replace = sOut
End Function

Labels: , ,


posted by ruffin at 8/30/2009 06:27:00 PM
Sunday, August 16, 2009

(trilingal, I am, yeeeeeees.)

Option Explicit

Public Sub createSql()
Dim i As Integer
Dim strTemp As String
Dim strCname As String
Dim strUserName As String


For i = 2 To 65

strCname = Cells(i, 1).Value
strUserName = Cells(i, 2).Value
'insanely important IP removed.
strTemp = strTemp & f(strCname, strUserName)

Next i
frmTemp.txtCode.Text = strTemp
frmTemp.Show
End Sub


Yes, yes, I already knew all this, but if you only use Cells(rows, columns) once every three or four years, it's good to have a refresher.

Labels: , , ,


posted by ruffin at 8/16/2009 12:29:00 PM

<< Older | Newer >>


Support freedom
All posts can be accessed here:


Just the last year o' posts:

URLs I want to remember:
* Atari 2600 programming on your Mac
* joel on software (tip pt)
* Professional links: resume, github, paltry StackOverflow * Regular Expression Introduction (copy)
* The hex editor whose name I forget
* JSONLint to pretty-ify JSON
* Using CommonDialog in VB 6 * Free zip utils
* git repo mapped drive setup * Regex Tester
* Read the bits about the zone * Find column in sql server db by name
* Giant ASCII Textifier in Stick Figures (in Ivrit) * Quick intro to Javascript
* Don't [over-]sweat "micro-optimization" * Parsing str's in VB6
* .ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture); (src) * Break on a Lenovo T430: Fn+Alt+B
email if ya gotta, RSS if ya wanna RSS, (?_?), ยข, & ? if you're keypadless


Powered by Blogger etree.org Curmudgeon Gamer badge
The postings on this site are [usually] my own and do not necessarily reflect the views of any employer, past or present, or other entity.