excel question

Option Explicit
Dim ws As Workspace
Dim Excelsheet As Object
Dim Worksheets
Dim Worksheet As Integer
Dim sSql$

Private Sub cmdPrint_Click()

Dim iRCnt%
Dim db As Database
Dim rs As Recordset
Dim n As Integer
Dim XName1, XName2, XName3, XName4, XName5, XName6, XName7, XName8, XName9, XName10 As String
Dim numcopies As Integer

Set Excelsheet = CreateObject("Excel.sheet")
Worksheet = 1


If txtStockNo.Text = "" Then
  MsgBox "Please enter a stockno to print"
  Exit Sub
End If


If CheckExists(Val(txtStockNo.Text)) = True Then

Set ws = Workspaces(0)
Set db = OpenDatabase("c:\stock.mdb")

sSql = "select * from Description, Product, Received_On, Issued_On, Balance where  " _
      + "val(Description.StockCardNo) = """ & Val(txtStockNo) & """ " _
      & "and Description.StockCardNo = Product.StockCardNo and Product.ProductID = Received_On.ProductID and Received_On.ProductID = Issued_On.ProductID and Received_On.ProductID = Balance.ProductID order by Product.ProductID and Description.StockCardNo "

' & "and Product.Specifications = '" + txtSpecs.Text + "' " _

Set rs = db.OpenRecordset(sSql)


Set DtaPrint.Recordset = rs
rs.MoveLast
iRCnt = rs.RecordCount

With Excelsheet
 If Worksheet > 1 Then
   .Worksheets.Add
   .Worksheets.PageSetup.LeftMargin = .Application.InchesToPoints(0.25)
   .Worksheets.RightMargin = .Application.InchesToPoints(0.25)
   .Worksheets.TopMargin = .Application.InchesToPoints(1)
   .Worksheets.BottomMargin = .Application.InchesToPoints(1)
   .Worksheets.HeaderMargin = .Application.InchesToPoints(0.5)
   .Worksheets.FooterMargin = .Application.InchesToPoints(0.5)
   
 End If

.Application.Visible = True


If iRCnt = 0 Then Exit Sub

.Worksheets(1).PageSetup.LeftMargin = .Application.InchesToPoints(0.25)
.Worksheets(1).PageSetup.RightMargin = .Application.InchesToPoints(0.25)
.Worksheets(1).PageSetup.TopMargin = .Application.InchesToPoints(1)
.Worksheets(1).PageSetup.BottomMargin = .Application.InchesToPoints(1)
.Worksheets(1).PageSetup.HeaderMargin = .Application.InchesToPoints(0.5)
.Worksheets(1).PageSetup.FooterMargin = .Application.InchesToPoints(0.5)

.Worksheets(1).range("D2").Value = "Mathematics, Science & Computing Center"
.Worksheets(1).range("D2").Font.fontstyle = "Bold"
'.Worksheets(1).range("D2").HorizontalAlignment = xlCenter
 
.Worksheets(1).range("D2").shrinktofit = False

.Worksheets(1).range("H2").Value = "MSC"
.Worksheets(1).range("H2").Font.fontstyle = "Bold"
'.Worksheets(1).range("H2").HorizontalAlignment = xlCenter

.Worksheets(1).range("I2").Value = txtStockNo.Text

.Worksheets(1).range("D3").Value = "Stock Record Card"
.Worksheets(1).range("D3").Font.fontstyle = "Bold"
'.Worksheets(1).range("D3").HorizontalAlignment = xlCenter

.Worksheets(1).range("A5").Value = "Description"
.Worksheets(1).range("B5").Value = GetDesc(Val(txtStockNo))

.Worksheets(1).range("C7").Value = "Receipts"
'.Worksheets(1).range("C7").HorizontalAlignment = xlCenter

.Worksheets(1).range("G7").Value = "Issues"
.Worksheets(1).range("I7").Value = "Balance"
.Worksheets(1).range("J7").Value = "Remarks"

.Worksheets(1).range("A8").Value = "Date"
'.Worksheets(1).range("A8").HorizontalAlignment = xlCenter

.Worksheets(1).range("B8").Value = "Type"
'.Worksheets(1).range("B8").HorizontalAlignment = xlCenter

.Worksheets(1).range("C8").Value = "Bill/PO"
'.Worksheets(1).range("C8").HorizontalAlignment = xlCenter

.Worksheets(1).range("D8").Value = "Qty"
'.Worksheets(1).range("D8").HorizontalAlignment = xlCenter

.Worksheets(1).range("E8").Value = "U/Price"
'.Worksheets(1).range("E8").HorizontalAlignment = xlCenter

.Worksheets(1).range("F8").Value = "Date"
'.Worksheets(1).range("F8").HorizontalAlignment = xlCenter

.Worksheets(1).range("G8").Value = "Req/Loc"
'.Worksheets(1).range("G8").HorizontalAlignment = xlCenter

.Worksheets(1).range("H8").Value = "Qty"
'.Worksheets(1).range("H8").HorizontalAlignment = xlCenter

.Worksheets(1).range("I8").Value = "Qty"
'.Worksheets(1).range("I8").HorizontalAlignment = xlCenter

'.Worksheets(1).range("G8").Borders.LineStyle = xlContinuous
'.Worksheets(1).range("G8").Borders.ColorIndex = xlAutomatic
.Worksheets(1).range("H5").Borders.LineStyle = xlContinuous
.Worksheets(1).range("H5").Borders.ColorIndex = xlAutomatic

rs.MoveFirst

For n = 0 To iRCnt - 1
 XName1 = "A" + Format(n + 10)
 XName2 = "B" + Format(n + 10)
 XName3 = "C" + Format(n + 10)
 XName4 = "D" + Format(n + 10)
 XName5 = "E" + Format(n + 10)
 XName6 = "F" + Format(n + 10)
 XName7 = "G" + Format(n + 10)
 XName8 = "H" + Format(n + 10)
 XName9 = "I" + Format(n + 10)
 XName10 = "J" + Format(n + 10)
 
 
.Worksheets(1).range(XName1).Value = rs.Fields("DateReceived")
.Worksheets(1).range(XName2).Value = rs.Fields("Specifications")
.Worksheets(1).range(XName3).Value = rs.Fields("BillNumber")
.Worksheets(1).range(XName4).Value = rs.Fields("QtyReceived")
.Worksheets(1).range(XName5).Value = rs.Fields("UnitPrice")
.Worksheets(1).range(XName6).Value = rs.Fields("DateIssued")
.Worksheets(1).range(XName7).Value = rs.Fields("Requisitioner")
.Worksheets(1).range(XName8).Value = rs.Fields("QtyIssued")
.Worksheets(1).range(XName9).Value = rs.Fields("QuantityLeft")
.Worksheets(1).range(XName10).Value = rs.Fields("Remarks")



.Worksheets(1).range(XName1).Select
.Worksheets(1).range(XName1).NumberFormat = "dd/mm/yy"

.Worksheets(1).range(XName6).Select
.Worksheets(1).range(XName6).NumberFormat = "dd/mm/yy"

.Worksheets(1).range(XName5).Select
.Worksheets(1).range(XName5).NumberFormat = "$#,##0.00;[Red]$#,##0.00"

.Worksheets(1).Columns("A:A").columnwidth = 7.14
.Worksheets(1).Columns("B:B").columnwidth = 11.86
.Worksheets(1).Columns("C:C").columnwidth = 11.29
.Worksheets(1).Columns("D:D").columnwidth = 4
.Worksheets(1).Columns("E:E").columnwidth = 7.29
.Worksheets(1).Columns("F:F").columnwidth = 7.43
.Worksheets(1).Columns("G:G").columnwidth = 14.86
.Worksheets(1).Columns("H:H").columnwidth = 4.29
.Worksheets(1).Columns("I:I").columnwidth = 6.86
.Worksheets(1).Columns("J:J").columnwidth = 17

rs.MoveNext
Next n

numcopies = 1

Worksheet = Worksheet + 1

Excelsheet.SaveAs "c:\Bookie.xls"
'Excelsheet.printout , , numcopies
Excelsheet.Application.Quit
'Kill "c:\Bookie.xls"

Set Excelsheet = Nothing

End With
     
End If

 
     
     
End Sub




i tried worksheets(!).range("A2").select.horizontalAlignment = xlCenter
and it said xlCenter is a undefine variable
also xlcontinous and xlautomatic is undefine...
can anyone solve this problem?
nightrageAsked:
Who is Participating?
 
lbkConnect With a Mentor Commented:
This is quite a bit of code, and I doubt to have the time to go through all of it, but considering you're late binding excel objects, it seem you didn't reference the Excel object library, which would indeed result in enumeration being unknown.

Try referencing the Excel object library... good luck

PS, once you did, you won't need createobject() anymore, but can use NEW WorkSheet instead, etc
0
 
frazerCommented:
Hi,

Dim XName1, XName2, XName3, XName4, XName5, XName6, XName7, XName8, XName9, XName10 As String

note:- in the above line, i am guessing that you want all variables to be strings....

The way you are declaring them all are variants except XName10

You must declare each variable individually.

You can use the shortcuts..

i.e.

&=Long
$=String
%=Integer.....

Hope this helps

Frazer
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.