B
Bill
Hi,
I am asking the question to see if I am going about some
code ok or to see if the way I am doing it is viable.
I am having problems in the degugging part with getting
runtine errors 91 saying object variable or with block
not set possibly because the excel sheet is losing focus
but how do i keep it in focus?.
I have pasted the complete code here,
My latest error is at the line , Set rng1 = xlsheet.Range
("b4:b75")which is a run time error 91.
What comments can you give me .
My experience is eclectic and self taught.
Option Compare Database
Option Explicit
Private Module
'set up variables for working with excel.
Dim xlapp As Object 'Excel application Object
Dim xlbook As Object 'Excel object-workbook
Dim xlsheet As Object 'excel worksheet within
the workbook
'set other module level variables
Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range
Dim rng4 As Range
Dim rng5 As Range
Dim rng6 As Range
Dim rng7 As Range
Dim rng8 As Range
Dim rng11 As Range
Dim rng21 As Range
Dim rng31 As Range
Dim rng41 As Range
Dim c As Integer
Dim res As Variant
Dim res2 As Variant
Dim res3 As Variant
Dim z As Variant
Dim cell As Range
Function TubeSelect()
Call Tube_Price
End Function
Private Sub Tube_Price()
'extract data and display/update access form.
'this will have to be a dlookup or vlookup in the
excel spreadsheet
'to locate values possibly via hard wiring.
'then copy/update those values to the access form.
'use case select in the tube Size to locate columns
in the spreadsheet
'this will act as a selection process
Select Case Forms![tubeselectform]!Frame110 '
tube size Me![tube size].
' each number in Case IS - represents a
diameter of pipe in millimetres.
Case Is = 50
Call Open_Excel
With xlapp
Set rng1 = xlsheet.Range("b4:b75")
xlsheet.Range("b4").Activate
End With
Price_2way
Case Is = 65
Call Open_Excel
With xlapp
Set rng2 = xlsheet.Range("c4:c75")
xlsheet.Range("c4").Activate
End With
Price_2way
Case Is = 75
Call Open_Excel
With xlapp
Set rng3 = xlsheet.Range("d4:d75")
xlsheet.Range("d4").Activate
End With
Price_2way
Case Is = 100
Call Open_Excel
With xlapp
Set rng4 = xlsheet.Range("e4:e75")
xlsheet.Range("e4").Activate
End With
MsgBox "Ok back in main. Ok to here",
vbOKOnly
Price_2way
Case Is = 125
Call Open_Excel
With xlapp
Set rng5 = xlsheet.Range("f4:f75")
xlsheet.Range("f4").Activate
End With
Price_2way
Case Is = 150
Call Open_Excel
With xlapp
Set rng6 = xlsheet.Range("g4:g75")
xlsheet.Range("g4").Activate
End With
Price_2way
Case Is = 200
Call Open_Excel
With xlapp
Set rng7 = xlsheet.Range("h4:h75")
xlsheet.Range("h4").Activate
End With
Price_2way
Case Is = 300
Call Open_Excel
With xlapp
Set rng8 = xlsheet.Range("i4:i75")
xlsheet.Range("i4").Activate
End With
Price_2way
End Select
'close excel and destroy object variables
'if we started this instance, our code uses the
'quit method to close the instance
xlapp.Quit
Set xlsheet = Nothing
Set xlbook = Nothing
Set xlapp = Nothing
End Sub
Private Sub Price_2way() ' pricing subroutine common
'look at the material option
If (Forms![tubeselectform]![Grade] = 1)
Then 'non sanitary mild steel
c = -3
ElseIf (Forms![tubeselectform]!
[Grade] = 2) Then ' non sanitary 304
c = -4
ElseIf (Forms![tubeselectform]!
[Grade] = 3) Then '316 stst option.
c = -8
Else
'do nothing
End If
z = ""
z = cell.Value
Set rng11 = xlsheet.ActiveCell.Offset
(c, 0).Find(what:=z)
res = ""
res = rng11
'rng11 is the value to be copied to
field Forms![tubeselectform]![Price_Tube_Size)
Forms![tubeselectform]!
[Price_Tube_Size] = res
'now look at the Cover option
If (Forms![tubeselectform]![Cover] = 2)
Then 'cover is 304 ss
c = -30
ElseIf (Forms![tubeselectform]!
[Cover] = 3) Then ' cover is 316 ss
c = -31
Else
c = -1
End If
z = ""
z = cell.Value
Set rng21 = xlsheet.ActiveCell.Offset
(c, 0).Find(what:=z)
res2 = ""
res2 = rng21
'rng21 is the value to be copied to
field Forms![tubeselectform]![Price_Cover_Option]and will
be numeric.
Forms![tubeselectform]!
[Price_Cover_Option] = res2
'now look at the Controller option
If (Forms![tubeselectform]![DC] = True)
Then
c = -29
ElseIf (Forms![tubeselectform]![DC] =
False) Then
c = -28
Else
'do nothing
End If
z = ""
z = cell.Value
Set rng31 = xlsheet.ActiveCell.Offset
(c, 0).Find(what:=z)
res3 = ""
res3 = rng31
'rng31 is the value to be copied to
field Forms![tubeselectform]![Price_Prologic)
Forms![tubeselectform]!
[Price_Prologic] = res3
End Sub
Private Sub Open_Excel()
'Find and open the Excel workbook with
'the pricing for the variuos Selector sizes.
'The Workbook does not have to be visible.
'set up variables for working with excel.
Dim xlapp As excel.Application 'Excel
application Object
Dim xlbook As excel.Workbook 'Excel object-
workbook
Dim xlsheet As excel.Sheets 'excel worksheet
within the workbook
'create an Excel instance i.e set up an active
instance
'use an existing instance if there is one;otherwise
'create a new instance
On Error Resume Next
Set xlapp = GetObject(, "Excel.application")
If Err.Number = 429 Then
'excel isn't running: create Excel Instance
Err.Clear
Set xlapp = CreateObject("Excel.Application")
End If
'Open relevant workbook and page
xlapp.Visible = False
xlbook = xlapp.Workbook.Open("C:\My
Documents\2WAYPRICES.xls")
xlapp.Sheets(2).Select
MsgBox "Ok To Here", vbOKOnly
End Sub
Warm Regards
Bill
I am asking the question to see if I am going about some
code ok or to see if the way I am doing it is viable.
I am having problems in the degugging part with getting
runtine errors 91 saying object variable or with block
not set possibly because the excel sheet is losing focus
but how do i keep it in focus?.
I have pasted the complete code here,
My latest error is at the line , Set rng1 = xlsheet.Range
("b4:b75")which is a run time error 91.
What comments can you give me .
My experience is eclectic and self taught.
Option Compare Database
Option Explicit
Private Module
'set up variables for working with excel.
Dim xlapp As Object 'Excel application Object
Dim xlbook As Object 'Excel object-workbook
Dim xlsheet As Object 'excel worksheet within
the workbook
'set other module level variables
Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range
Dim rng4 As Range
Dim rng5 As Range
Dim rng6 As Range
Dim rng7 As Range
Dim rng8 As Range
Dim rng11 As Range
Dim rng21 As Range
Dim rng31 As Range
Dim rng41 As Range
Dim c As Integer
Dim res As Variant
Dim res2 As Variant
Dim res3 As Variant
Dim z As Variant
Dim cell As Range
Function TubeSelect()
Call Tube_Price
End Function
Private Sub Tube_Price()
'extract data and display/update access form.
'this will have to be a dlookup or vlookup in the
excel spreadsheet
'to locate values possibly via hard wiring.
'then copy/update those values to the access form.
'use case select in the tube Size to locate columns
in the spreadsheet
'this will act as a selection process
Select Case Forms![tubeselectform]!Frame110 '
tube size Me![tube size].
' each number in Case IS - represents a
diameter of pipe in millimetres.
Case Is = 50
Call Open_Excel
With xlapp
Set rng1 = xlsheet.Range("b4:b75")
xlsheet.Range("b4").Activate
End With
Price_2way
Case Is = 65
Call Open_Excel
With xlapp
Set rng2 = xlsheet.Range("c4:c75")
xlsheet.Range("c4").Activate
End With
Price_2way
Case Is = 75
Call Open_Excel
With xlapp
Set rng3 = xlsheet.Range("d4:d75")
xlsheet.Range("d4").Activate
End With
Price_2way
Case Is = 100
Call Open_Excel
With xlapp
Set rng4 = xlsheet.Range("e4:e75")
xlsheet.Range("e4").Activate
End With
MsgBox "Ok back in main. Ok to here",
vbOKOnly
Price_2way
Case Is = 125
Call Open_Excel
With xlapp
Set rng5 = xlsheet.Range("f4:f75")
xlsheet.Range("f4").Activate
End With
Price_2way
Case Is = 150
Call Open_Excel
With xlapp
Set rng6 = xlsheet.Range("g4:g75")
xlsheet.Range("g4").Activate
End With
Price_2way
Case Is = 200
Call Open_Excel
With xlapp
Set rng7 = xlsheet.Range("h4:h75")
xlsheet.Range("h4").Activate
End With
Price_2way
Case Is = 300
Call Open_Excel
With xlapp
Set rng8 = xlsheet.Range("i4:i75")
xlsheet.Range("i4").Activate
End With
Price_2way
End Select
'close excel and destroy object variables
'if we started this instance, our code uses the
'quit method to close the instance
xlapp.Quit
Set xlsheet = Nothing
Set xlbook = Nothing
Set xlapp = Nothing
End Sub
Private Sub Price_2way() ' pricing subroutine common
'look at the material option
If (Forms![tubeselectform]![Grade] = 1)
Then 'non sanitary mild steel
c = -3
ElseIf (Forms![tubeselectform]!
[Grade] = 2) Then ' non sanitary 304
c = -4
ElseIf (Forms![tubeselectform]!
[Grade] = 3) Then '316 stst option.
c = -8
Else
'do nothing
End If
z = ""
z = cell.Value
Set rng11 = xlsheet.ActiveCell.Offset
(c, 0).Find(what:=z)
res = ""
res = rng11
'rng11 is the value to be copied to
field Forms![tubeselectform]![Price_Tube_Size)
Forms![tubeselectform]!
[Price_Tube_Size] = res
'now look at the Cover option
If (Forms![tubeselectform]![Cover] = 2)
Then 'cover is 304 ss
c = -30
ElseIf (Forms![tubeselectform]!
[Cover] = 3) Then ' cover is 316 ss
c = -31
Else
c = -1
End If
z = ""
z = cell.Value
Set rng21 = xlsheet.ActiveCell.Offset
(c, 0).Find(what:=z)
res2 = ""
res2 = rng21
'rng21 is the value to be copied to
field Forms![tubeselectform]![Price_Cover_Option]and will
be numeric.
Forms![tubeselectform]!
[Price_Cover_Option] = res2
'now look at the Controller option
If (Forms![tubeselectform]![DC] = True)
Then
c = -29
ElseIf (Forms![tubeselectform]![DC] =
False) Then
c = -28
Else
'do nothing
End If
z = ""
z = cell.Value
Set rng31 = xlsheet.ActiveCell.Offset
(c, 0).Find(what:=z)
res3 = ""
res3 = rng31
'rng31 is the value to be copied to
field Forms![tubeselectform]![Price_Prologic)
Forms![tubeselectform]!
[Price_Prologic] = res3
End Sub
Private Sub Open_Excel()
'Find and open the Excel workbook with
'the pricing for the variuos Selector sizes.
'The Workbook does not have to be visible.
'set up variables for working with excel.
Dim xlapp As excel.Application 'Excel
application Object
Dim xlbook As excel.Workbook 'Excel object-
workbook
Dim xlsheet As excel.Sheets 'excel worksheet
within the workbook
'create an Excel instance i.e set up an active
instance
'use an existing instance if there is one;otherwise
'create a new instance
On Error Resume Next
Set xlapp = GetObject(, "Excel.application")
If Err.Number = 429 Then
'excel isn't running: create Excel Instance
Err.Clear
Set xlapp = CreateObject("Excel.Application")
End If
'Open relevant workbook and page
xlapp.Visible = False
xlbook = xlapp.Workbook.Open("C:\My
Documents\2WAYPRICES.xls")
xlapp.Sheets(2).Select
MsgBox "Ok To Here", vbOKOnly
End Sub
Warm Regards
Bill