View Single Post
  #7   Report Post  
Posted to microsoft.public.excel.programming
Calligra[_2_] Calligra[_2_] is offline
external usenet poster
 
Posts: 17
Default Message box causing error in code

Ed,

Thanks for the assistance. I did find the error though and wanted to
post it for future reference. It turns out that when I initialized the
"Book" and then opened the secondary workbook, it lost it's
ActiveWorkbook setting. Although the Local and Watch windows don't show
any change with the object, I found that if I clicked on the plus sign
next to the Book in the watch window after the code stepped through
"Book.Activate", Excel would crash. If I clicked on the Book's plus
sign after the code stepped through the "With Book.frmLogin", the
properties of book had changed. I found that, being as I didn't need
the other Workbook to remain open after it got the information, if I
closed the secondary workbook prior to calling the Book's frmLogin, the
code will accept the MessageBox and exit without error. Below is the
revised frmLogin Code.

Private Sub UserForm_Initialize()
Dim Counter As Integer
Dim ctrl As Control
Dim iCount As Integer
Dim Book As Excel.Workbook
Dim NameCounter As Integer
Dim ans As String
Dim iCellValue As String


Set Book = Workbooks("Group 22--EVComm4.xls")
Application.ScreenUpdating = False
'Open workbook
Workbooks.Open Filename:=ThisWorkbook.Path & "\Data Doc.xls"
Book.Activate
Application.ScreenUpdating = True
' Find last used row
With Workbooks("Data Doc.xls").Sheets("Sheet1")
Counter = 1
iCellValue = .Cells(Counter, 2).Value
If iCellValue < "" Then
Do
Counter = Counter + 1
Loop While .Cells(Counter, 2).Value < ""
End If
Counter = Counter - 1
If ((.Cells(Counter, 1).Value < Application.UserName) And
(.Cells(Counter, 2).Value < Book.Name) And (.Cells(Counter, 1).Value <
"")) Then
' if form is empty then
Workbooks("Data Doc.xls").Close SaveChanges:=True

With frmLogIn
.TextBox1.Text = Application.UserName
.TextBox2.Text = Now
.TextBox1.SetFocus
OptionButton23.Enabled = False
OptionButton24.Enabled = False
TextBox3.Enabled = False
End With
Else
' Read the values and place into form
For NameCounter = Counter To 2 Step -1
If ((.Cells(NameCounter, 1).Value < "") And
((NameCounter) < 2)) Then
TextBox1.Text = .Cells(Counter, 1).Value
End If
Next NameCounter
If TextBox1.Text = "" Then
TextBox1.Text = Application.UserName
End If

TextBox2.Text = .Cells(Counter, 3).Value
TxtVersion.Text = .Cells(Counter, 13).Value
TextBox4.Text = .Cells(Counter, 14).Value
TextBox5.Text = .Cells(Counter, 9).Value
TextBox6.Text = .Cells(Counter, 10).Value

For iCount = 1 To 31
Set ctrl = Controls("OptionButton" & iCount)
If iCount < 8 Then
If ctrl.Caption = CStr(.Cells(Counter, 6).Value)
Then
ctrl.Value = True
End If
ElseIf ((iCount 7) And (iCount < 15)) Then
If ((ctrl.Caption = .Cells(Counter, 7).Value) And
(iCount < 14)) Then
ctrl.Value = True
ElseIf ((TextBox3 = .Cells(Counter, 7).Value) And
(iCount = 14)) Then
ctrl.Value = True
TextBox3.Value = .Cells(Counter, 7).Value
End If
ElseIf ((iCount 14) And (iCount < 22)) Then
If ((ctrl.Caption = .Cells(Counter, 8).Value) And
(iCount < 21)) Then
ctrl.Value = True
OptionButton23.Enabled = False
OptionButton24.Enabled = False
ElseIf ((OptionButton23.Caption = .Cells(Counter,
8).Value) And (iCount = 21)) Then
ctrl.Value = True
OptionButton23.Value = True
ElseIf ((OptionButton24.Caption = .Cells(Counter,
8).Value) And (iCount = 21)) Then
ctrl.Value = True
OptionButton24.Value = True
End If
ElseIf ((iCount 24) And (iCount < 28)) Then
If ctrl.Caption = .Cells(Counter, 11).Value Then
ctrl.Value = True
End If
Else
If ctrl.Caption = .Cells(Counter, 12).Value Then
ctrl.Value = True
End If
End If
Next iCount
' Close workbook
Workbooks("Data Doc.xls").Close SaveChanges:=False
frmLogIn.Show
ans = MsgBox("Is the information contained within this form
still correct?", vbYesNo)
End If
End With
Set Book = Nothing
If ((ans < "") And (ans = vbYes)) Then
Call CmdOK_Click
End If
End Sub




*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!