Web development India freelance website designer developer India SEO

I recently needed to write an app that could take the results from a SQL query and output it in a report. Trick was that the
client machine did not have any Office components installed. So I created the Notepad reporter.

This was written to a Sybase database using ADO through an ODBC driver. You may be able to use a different type cursor
using a native driver. I found that using a static cursor treated everything, even integers, as strings and stripped the trailing
zeros.

Code:


Option Explicit
Dim gCn As ADODB.Connection
Dim rsTemp As Recordset

Public Function TableToNotePad(sSource _
As String, sFile As String) As Boolean

Dim sHeader As String
Dim sRow As String
Dim i As Integer
Set gCn = New ADODB.Connection
MousePointer = vbHourglass
With gCn
.ConnectionTimeout = 300
.ConnectionString = "Driver={Sybase System11};SRVR=DBSVR;DB=database;UID=user;PWD=password"
.Open
End With
Set rsTemp = New ADODB.Recordset
With rsTemp
.ActiveConnection = gCn
.CursorType = adOpenDynamic
.LockType = adLockReadOnly
.CursorLocation = adUseServer
.Open (sSource)

' Make sure you have records to write
If rsTemp.EOF And rsTemp.BOF Then
TableToNotePad = False
.Close
Set rsTemp = Nothing
If MsgBox("There were no records returned. Click OK to try again, Cancel to quit.", vbOKCancel, "No Records") = vbOK Then
Close #1 ' Target file is complete
Set rsTemp = Nothing
gCn.Close
Set gCn = Nothing
MousePointer = vbNormal
Exit Function
Else
End
End If
End If

' Create new report file
Open sFile For Output As #1
' Create a header row using the field names and column widths
For i = 0 To .Fields.Count - 1
If i = 0 Then
sHeader = .Fields(i).Name
Else
sHeader = sHeader & Space(.Fields(i).DefinedSize - Len(.Fields(i).Name)) & .Fields(i).Name
End If
Next i
Print #1, sHeader

' Loop through the table and write data rows
.MoveFirst
Do Until .EOF

'****Put a case construct here to trap for blobs and to only use the replace on text fields.

For i = 0 To .Fields.Count - 1
If i = 0 Then
sRow = .Fields(i).Value & Space(.Fields(i).DefinedSize - Len(.Fields(i).Value))
Else
If .Fields(i).Type = adLongVarChar Then
sRow = sRow & Space(.Fields(i).DefinedSize - Len(.Fields(i).Value))
End If
If .Fields(i).Type = adNumeric Or .Fields(i).Type = adUnsignedTinyInt Then
sRow = sRow & .Fields(i).Value & Space(.Fields(i).DefinedSize - Len(.Fields(i).Value))
Else
If Len(.Fields(i).Value) <> 0 Then
sRow = sRow & .Fields(i).Value & Space(.Fields(i).DefinedSize - Len(.Fields(i).Value))
Else
sRow = sRow & Space(.Fields(i).DefinedSize - Len(.Fields(i).Value))
End If
End If
End If
Next i
Print #1, sRow
.MoveNext
Loop
.Close
End With
Close #1 ' Target file is complete
Set rsTemp = Nothing
gCn.Close
Set gCn = Nothing
MousePointer = vbNormal
TableToNotePad = True
TableToNotePad_Exit:
Exit Function
TableToNotePad_Err:
Resume Next
End Function

Private Sub cmdEnd_Click()
Set rsTemp = Nothing
Set gCn = Nothing
MousePointer = vbNormal
End
End Sub

Private Sub cmdReport_Click()
Dim strSQL As String
Dim Note As Integer
strSQL = "select lastname, firstname from person where lastname like 'cap%'"
If Len(strSQL) > 1 Then
If TableToNotePad(strSQL, "c:Report.txt") = True Then
Note = Shell("notepad c:Report.txt", vbMaximizedFocus)
End If
Else
MsgBox "There is no report to create"
Exit Sub
End If
End Sub

The notepad reporter

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300

India web developer web development India | Freelance web development ecommerce web developer | Prayagasoft - web designer India, Ecommerce developer india, Ecommerce design