It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming Visual Basic Forum

Collect data from table and write to a file

Moderator: dafunkymunky

Collect data from table and write to a file

Postby uadm26 on Mon Mar 09, 2009 11:50 am

Hi,
I need to do a vbscript or a VB that connect to a database, execute a sql statement and send the output to a file. I have the done something so far, but I can't finish it. Can someone help me?

Dim cmd
Dim rs
Dim params
Dim param
Dim ArgObj, sunumber

Set ArgObj = WScript.Arguments
set cn=createobject("ADODB.Connection")
set cmd=createobject("ADODB.Command")
set rs=createobject("ADODB.Recordset")

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run("%comspec% /c <Console Command>")

cn.Provider = "SQLOLEDB"
cn.Open "Data Source = xpto", "User", "User"

Set cmd.ActiveConnection = cn
cmd.CommandText = "SELECT (*) FROM table"
uadm26
 
Posts: 1
Joined: Tue Dec 18, 2007 10:51 am

Re: Collect data from table and write to a file

Postby White95dp on Mon Mar 09, 2009 6:15 pm

Private Sub DisplayDepartments()
On Error GoTo MeError
Dim iSize(5000) As Integer
Dim StrSql As String
Set nDepartment = New Collection
'
' Query to read all entries from table called Department
'
StrSql = "SELECT * FROM Department order by DepartmentID"
'
' Open Database
'
Set DB = OpenDatabase(DatabaseName)
Set qdfLocal = DB.CreateQueryDef("")
qdfLocal.sql = StrSql
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim iRowloc As Integer
Dim cMsg As String
iRowloc = 1
Set rstTopFive = qdfLocal.OpenRecordset()
'
' j=Number of records
'
j = rstTopFive.Fields.Count
If j = 0 Then GoTo MeClose
iMax = j - 1
'
' Read all the Fields Names
'
cMsg = ""
For i = 0 To j - 1
k = Len(rstTopFive.Fields(i).Name)
cMsg = cMsg & rstTopFive.Fields(i).Name '& Space(iSize(i) - k)
'iSize(i) = Len(rstTopFive.Fields(i).Name)
If i < j - 1 Then cMsg = cMsg & Chr(9)
' LblFld(i).Caption = rstTopFive.Fields(i).Name
Next
FlexDepartments.Clear
FlexDepartments.Rows = 1
ComUserDepartment.Clear
cMsg = cMsg & " "
FlexDepartments.FormatString = cMsg

'
' Read all the Entries
'
For i = 0 To 49
DepartmentName(i) = "?"
Next
With rstTopFive
Do While Not .EOF
cMsg = ""
Set cDepartment = New clsDepartment
For i = 0 To j - 1
cMsg = cMsg & rstTopFive.Fields(i)
If i < j - 1 Then cMsg = cMsg & Chr(9)
Next

FlexDepartments.AddItem cMsg, iRowloc
.MoveNext
Loop
.Close
End With
MeClose:
DB.Close

Exit Sub
'
' For Error - close database
'
MeError:
MsgBox Error$()
DB.Close
End Sub
White95dp
 
Posts: 7
Joined: Mon Mar 09, 2009 12:52 am


Who is online

Users browsing this forum: No registered users and 5 guests