Thursday, September 10, 2009

How to export from Excel to a CSV file with double quotes on each field

The default "save as CSV" option from Excel does not include any quotes on the fields, therefore a macro is needed in this case:

Sub CSVFile()

Dim SrcRg As Range
Dim CurrRow As Range
Dim CurrCell As Range
Dim CurrTextStr As String
Dim ListSep As String
Dim FName As Variant
FName = Application.GetSaveAsFilename("", "CSV File (*.csv), *.csv")

If FName <> False Then
ListSep = Application.International(xlListSeparator)
If Selection.Cells.Count > 1 Then
Set SrcRg = Selection
Else
Set SrcRg = ActiveSheet.UsedRange
End If
Open FName For Output As #1
For Each CurrRow In SrcRg.Rows
CurrTextStr = ""
For Each CurrCell In CurrRow.Cells
CurrTextStr = CurrTextStr & """" & CurrCell.Value & """" & ListSep
Next
While Right(CurrTextStr, 1) = ListSep
CurrTextStr = Left(CurrTextStr, Len(CurrTextStr) - 1)
Wend
Print #1, CurrTextStr
Next
Close #1
End If
End Sub

The original post can be retrieved here.

Monday, September 7, 2009

The two most important differences between temp table and table variable

Since this can be one of the mostly asked interview questions, I better have the answers here instead of doing another round of Googling.

1. table variable does not support transaction while temp table does
2. table variable does not support constraints while temp table does

A full expiation is here.

SourceSafe plug-in setting for Visual Studio

This might sound very stupid, but it took quite some time for me to figure out why my Visual Studio failed to connect to our SourceSafe database on which it turns out that the internet connection is switched off by default(On Server, when installing VSS, select Custom Install and make sure you select the "Server Components" or at least the HTTP Remote Access component; they are not installed in Default installation confuguration). While the Visual Studio by default connects to internet SSL connection based database, my multiple attempts failed without providing me too much of a clue since a VNC and a URL in a LAN setting do not vary from each other too much. So after checking an interesting article, the solution is plain simple as shown in the below screen shot in Visual Studio's options.