Macro problem

Noz2k

Well-known Member
Joined
Mar 15, 2011
Messages
688
I have 2 command buttons, 1 which opens a userform and enables the user to scroll through the records and amend them, and another which opens the userform but only allows the user to make a new entry.

This is done by enabling/disabling a combobox.

The problem I am having is that I have to seemingly run the macro twice for it to take effect. Any ideas why this might be?

Here are the 2 codes

Editable
Code:
Sub ViewForm()
Dim pword As String
pword = InputBox("Please enter the password:")
If pword <> "mypassword" Then
MsgBox "Incorrect password"
Else
 
UserForm1.Show
 
End If
End Sub


Create New only
Code:
Sub NewForm()
UserForm1.Show
UserForm1.cboAccidentID.Enabled = False
End Sub
 

Some videos you may like

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).

JamesW

Well-known Member
Joined
Oct 30, 2009
Messages
1,197
Hi Noz,

Which macro do you have to run twice? Both of them?

Have you tried Enabling the combobox in the ViewForm Sub?
 

Noz2k

Well-known Member
Joined
Mar 15, 2011
Messages
688
Yes both of them.

I have, but the only difference it makes is that when I then close the form it generates an error on that line "cboAccidentID.Enabled = True"

"Run-time error '424':

Object Required"

Edit: Apologies that was because the line should have been UserForm1.cboAccidentID.Enabled = True

So no error now, but still have to run the macros twice
 
Last edited:

Noz2k

Well-known Member
Joined
Mar 15, 2011
Messages
688
So I had a couple of Me.EnableEvents in my Userform_Initialize which didn't need to be there (I'd left them from something I tried earlier).

So now it works, but strangely the wrong way around where

.Enabled = False, it seems to be enabled

and where

.Enabled = True, it seems to be disabled
 

JamesW

Well-known Member
Joined
Oct 30, 2009
Messages
1,197
Ahh I see now. Swap the cbo enabled line with the userform.show line:

Code:
    Dim pword As String
        pword = InputBox("Please enter the password:")
    If pword <> "a" Then
        MsgBox "Incorrect password"
    Else
        UserForm1.cboAccidentID.Enabled = True
        UserForm1.Show
    End If

Code:
    UserForm1.cboAccidentID.Enabled = False
    UserForm1.Show
 

Watch MrExcel Video

Forum statistics

Threads
1,127,910
Messages
5,627,588
Members
416,255
Latest member
amethystia

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top