Option Explicit
Private con As New ADODB.Connection
Public RS As New ADODB.Recordset
Public RS1 As New ADODB.Recordset
Public cmd As String
Dim ExlObj As New Excel.Application
Private Declare Function GetSystemMetrics Lib "user32" (ByVal Index As Long) As Long
'Public Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long
Private Sub MyDatacon()
If con.State = 1 Then con.Close
con.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=ISIS_157;Data Source=JASEEMAHMED"
con.Open
End Sub
Private Sub Command1_Click()
On Error Resume Next
Dim lc, NxtLine, k
Call MyDatacon
Set ExlObj = CreateObject("Excel.application")
ExlObj.Workbooks.Add
RS.Open "SELECT CUSTOMERNAME,Shopname,Contactperson, Address,Street,City,Phone,Fax from customer where customercode='" + Trim(List1.Text) + "'", con, adOpenStatic, adLockOptimistic
If Not RS.EOF Then
ExlObj.Visible = True
With ExlObj.ActiveSheet
.Cells(1, 3).Value = "Visual Basic Data in Vb"
.Cells(1, 3).Font.Name = "Verdana"
.Cells(1, 3).Font.Bold = True:
.Cells(4, 1).Value = "Customername": .Cells(4, 2).Value = "city"
.Cells(4, 3).Value = "Shopname": .Cells(4, 4).Value = "phone"
.Cells(4, 5).Value = "Contactperson": .Cells(4, 6).Value = "fax"
.Cells(4, 7).Value = "address"
End With
End If
For k = 1 To RS.Fields.Count
ExlObj.ActiveSheet.Cells(4, k).Font.Bold = True
ExlObj.ActiveSheet.Cells(4, k).Font.Color = vbWhite
Next
Set k = Nothing
NxtLine = 5
Do Until RS.EOF
For lc = 0 To RS.Fields.Count - 1
ExlObj.ActiveSheet.Cells(NxtLine, lc + 1).Value = RS.Fields(lc)
Next
RS.MoveNext
NxtLine = NxtLine + 1
Loop
ExlObj.ActiveCell.Worksheet.Cells(NxtLine, lc + 1).AutoFormat xlRangeAutoFormatList2, 0, 3, 1, True, True
ExlObj.ActiveCell.Worksheet.Cells(4, 1).Subtotal 4, xlSum, (6), 0, 0, xlSummaryBelow
End Sub
Private Sub Form_Load()
Call MyDatacon
RS.Open "SELECT DISTINCT Customercode from Customer ", con, adOpenStatic, adLockOptimistic
While Not RS.EOF
List1.AddItem RS.Fields(0).Value
RS.MoveNext
Wend
End Sub
customer codes are listed in list box, and when choosing any code and clicking command 1, it exports the sheet, where all the coloum names that i have written above are shown, but no any data is called from database, it it runs an infinite loop on
NxtLine = NxtLine + 1
please i need an argent help.