Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

excel question

Posted on 1998-08-16
2
Medium Priority
?
683 Views
Last Modified: 2008-02-01
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?
0
Comment
Question by:nightrage
2 Comments
 
LVL 1

Accepted Solution

by:
lbk earned 200 total points
ID: 1429130
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
 
LVL 3

Expert Comment

by:frazer
ID: 1429131
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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question