Please help me to solve the error in my code i have a problem with initializing the loop. please give me some advise thanks in advance.
My problem is, that i want to export data from Access into my Excel worksheet.
where first 3 columns in my Worksheet will contain the Product No, Product Code and Product Name. and i succeed to transfer this data, but after this process i want to transfer the query which holds the remaining quantity from my stock table and which holds some of same existing Product code which i have already transfered in my worksheet and find this same code in my whole first row,
for that i set the record set which work perfectly only problem occours when i want to loop my first column within range of my Worksheet and want to compare the Product code with the result of my query using recordset, if the Code exist in both i mean in my worksheet and in my query result, then i want to bring this data in my worksheet at the same row where this code exist and move my record set to next and repeat the same process until my record set reach to end of fie.
Here is the code. I have a problem in Block 3
=================================================
Private Sub hpseltoexcel_Click()
Dim xlBudgetSchedule As Excel.Application
Dim rwIndex As Integer
Dim currentRow As Integer
Dim currentColumn As Integer
Dim prodlist As Recordset
Dim realstock As Recordset
Dim dbs As Database
Dim title As String
Set dbs = CurrentDb
title = "BackOffice!"
'=======================================================
Block 1:
in this Block i'm setting up the heading of first three columns and transfering the data to my Excel Worksheet using ReocrdSet.
'========================================================
Set xlBudgetSchedule = New Excel.Application
xlBudgetSchedule.Visible = True
xlBudgetSchedule.Workbooks.add
xlBudgetSchedule.ActiveWindow.DisplayGridlines = True 'show grid lines
With xlBudgetSchedule.ActiveSheet
.Cells(2, 1).Value = "Inernal Code"
.Cells(2, 2).Value = "Vender Code"
.Cells(2, 3).Value = "Product Name"
End With
currentRow = 3
currentColumn = 1
Set prodlist = dbs.OpenRecordset("select * from products where (VenderID = 4)ORDER BY Hp_ProductName", dbOpenDynaset)
If prodlist.RecordCount > 0 Then
Do Until prodlist.EOF
With xlBudgetSchedule.ActiveSheet
.Cells(currentRow, currentColumn).Value = prodlist!productid
.Cells(currentRow, currentColumn + 1).Value = prodlist!Hp_ProductName
.Cells(currentRow, currentColumn + 2).Value = prodlist!product_name
.Cells(currentRow, currentColumn).HorizontalAlignment = xlLeft
currentRow = currentRow + 1
currentColumn = 1
End With
prodlist.MoveNext
Loop
End If
'=======================================================
Block 2:
in this Block i'm setting up the heading of 4th column and transfering the data to my Excel Worksheet Using RecordSet.
'========================================================
With xlBudgetSchedule.ActiveSheet
.Cells(2, 4).Value = "Stock Remaining"
End With
currentRow = 3
currentColumn = 4
Set realstock = dbs.OpenRecordset(" SELECT HPresterquery.ProductID, HPresterquery.SumOfquantity FROM HPresterquery", dbOpenDynaset)
If realstock.RecordCount > 0 Then
Do Until realstock.EOF
Dim lRow As Long
With xlBudgetSchedule.ActiveSheet
For lRow = Range("A65536").End(xlUp).Row To 1 Step -1
'=======================================================
Block 3:
Here i have problem i can not compare the resulf of my record set with in my workshee range column.
'========================================================
If Range("A" & lRow & ":A" & lRow).Find(realstock!productid, LookIn:=xlValues, lookat:=xlWhole) Then
.Cells(Rows(lRow), currentColumn).Value = realstock!SumOfquantity
End If
Next
End With
realstock.MoveNext
Loop
End If
realstock.Close
prodlist.Close
dbs.Close
'Caption stuff
xlBudgetSchedule.ActiveSheet.Name = "HPSellout"
xlBudgetSchedule.ActiveWindow.Caption = "HPSellout"
xlBudgetSchedule.Application.Caption = "HPSellout"
xlBudgetSchedule.Application.DisplayFormulaBar = True
xlBudgetSchedule.ActiveWindow.DisplayFormulas = True
xlBudgetSchedule.ActiveWindow.DisplayHeadings = True
xlBudgetSchedule.ActiveWindow.DisplayZeros = True
End Sub
==========================================
End of code
==========================================
Please put me on right track thank in advance.
My problem is, that i want to export data from Access into my Excel worksheet.
where first 3 columns in my Worksheet will contain the Product No, Product Code and Product Name. and i succeed to transfer this data, but after this process i want to transfer the query which holds the remaining quantity from my stock table and which holds some of same existing Product code which i have already transfered in my worksheet and find this same code in my whole first row,
for that i set the record set which work perfectly only problem occours when i want to loop my first column within range of my Worksheet and want to compare the Product code with the result of my query using recordset, if the Code exist in both i mean in my worksheet and in my query result, then i want to bring this data in my worksheet at the same row where this code exist and move my record set to next and repeat the same process until my record set reach to end of fie.
Here is the code. I have a problem in Block 3
=================================================
Private Sub hpseltoexcel_Click()
Dim xlBudgetSchedule As Excel.Application
Dim rwIndex As Integer
Dim currentRow As Integer
Dim currentColumn As Integer
Dim prodlist As Recordset
Dim realstock As Recordset
Dim dbs As Database
Dim title As String
Set dbs = CurrentDb
title = "BackOffice!"
'=======================================================
Block 1:
in this Block i'm setting up the heading of first three columns and transfering the data to my Excel Worksheet using ReocrdSet.
'========================================================
Set xlBudgetSchedule = New Excel.Application
xlBudgetSchedule.Visible = True
xlBudgetSchedule.Workbooks.add
xlBudgetSchedule.ActiveWindow.DisplayGridlines = True 'show grid lines
With xlBudgetSchedule.ActiveSheet
.Cells(2, 1).Value = "Inernal Code"
.Cells(2, 2).Value = "Vender Code"
.Cells(2, 3).Value = "Product Name"
End With
currentRow = 3
currentColumn = 1
Set prodlist = dbs.OpenRecordset("select * from products where (VenderID = 4)ORDER BY Hp_ProductName", dbOpenDynaset)
If prodlist.RecordCount > 0 Then
Do Until prodlist.EOF
With xlBudgetSchedule.ActiveSheet
.Cells(currentRow, currentColumn).Value = prodlist!productid
.Cells(currentRow, currentColumn + 1).Value = prodlist!Hp_ProductName
.Cells(currentRow, currentColumn + 2).Value = prodlist!product_name
.Cells(currentRow, currentColumn).HorizontalAlignment = xlLeft
currentRow = currentRow + 1
currentColumn = 1
End With
prodlist.MoveNext
Loop
End If
'=======================================================
Block 2:
in this Block i'm setting up the heading of 4th column and transfering the data to my Excel Worksheet Using RecordSet.
'========================================================
With xlBudgetSchedule.ActiveSheet
.Cells(2, 4).Value = "Stock Remaining"
End With
currentRow = 3
currentColumn = 4
Set realstock = dbs.OpenRecordset(" SELECT HPresterquery.ProductID, HPresterquery.SumOfquantity FROM HPresterquery", dbOpenDynaset)
If realstock.RecordCount > 0 Then
Do Until realstock.EOF
Dim lRow As Long
With xlBudgetSchedule.ActiveSheet
For lRow = Range("A65536").End(xlUp).Row To 1 Step -1
'=======================================================
Block 3:
Here i have problem i can not compare the resulf of my record set with in my workshee range column.
'========================================================
If Range("A" & lRow & ":A" & lRow).Find(realstock!productid, LookIn:=xlValues, lookat:=xlWhole) Then
.Cells(Rows(lRow), currentColumn).Value = realstock!SumOfquantity
End If
Next
End With
realstock.MoveNext
Loop
End If
realstock.Close
prodlist.Close
dbs.Close
'Caption stuff
xlBudgetSchedule.ActiveSheet.Name = "HPSellout"
xlBudgetSchedule.ActiveWindow.Caption = "HPSellout"
xlBudgetSchedule.Application.Caption = "HPSellout"
xlBudgetSchedule.Application.DisplayFormulaBar = True
xlBudgetSchedule.ActiveWindow.DisplayFormulas = True
xlBudgetSchedule.ActiveWindow.DisplayHeadings = True
xlBudgetSchedule.ActiveWindow.DisplayZeros = True
End Sub
==========================================
End of code
==========================================
Please put me on right track thank in advance.