Excel VB: get the value checked/unchecked for a checkbox - vba

I'm not able to get the value of a checkbox..
I've googled a lot but I really can't find the error.
Anyway this is my code:
If printa.Checked = True Then
objDoc.PrintOut
Else
bjDoc.Close
End If
The chechbox's name in the Userform is "printa".
I get the error that it can't find the method or the object.
Any advice?
Thanks
Dennis

The property you want to use is Value:
If printa.Value = True Then
objDoc.PrintOut
Else
objDoc.Close
End If
(There's also a typo in your objDoc.Close)

Related

MS Access vba code error with visible invisible VBA code

I have a filed called application. I have written code in vba to make the other field "application_txt" field visible and invisible if a particular value from application field is selected. But for this particulat field application when I write the below code in vba it throws the error. I dont understand why. It works fine for the other fields. Below is the code:
Can anyone please help me with this problem. Is it allowed to give application as field name in MS Access.
Many thanks,
Kiran Chapidi
This is not working:
Private Sub application_Click()
If application.Value = "5= others" Then
Me.application_txt.Visible = True
Else
Me.application_txt.Visible = False
End If
End Sub
This is working fine:
Private Sub Design_Click()
If Design.Value = "3= others" Then
Me.design_txt.Visible = True
Else
Me.design_txt.Visible = False
End If
End Sub

Runtime error 438 on remove dynamically added userform control

I've got the following code:
Private Sub cboA_change()
'Something to determine number of controls there should be, variable gC
'Something to determine number of controls there are, variable nC
'The first time the code runs, the following code runs:
For i = nC to gC
frmA.Frame1.Controls.Add("txtGroup" & i)
Next
'The second time the code runs, the following is executed:
For i = 7 To nC
Me.Frame1.Controls("txtGroup" & i).Remove 'ERROR HERE
Next
For i = nC to gC
frmA.Frame1.Controls.Add("txtGroup" & i)
Next
End Sub
Something like this, the code is way bigger and I tried to clear it up so if the structure doesn't seem right, that doesn't matter really.
I debugged the Add statement and I know there is a control added to the userform, called txtGroup7. However, when I later try to remove this control, I get Run-time Error 438: Object Doesn't Support This Property or Method. I tried changing the code to:
Me.Frame1.Controls.Remove ("txtGroup" & i)
But this didn't work either.
Can anybody point me in the right direction?
Edit:
I know the help says the following:
"This method deletes any control that was added at run time. However,
attempting to delete a control that was added at design time will
result in an error."
But since the control is added in run-time (dynamically, with VBA code) this shouldn't be a problem, right?
Edit 2:
I don't get why this works, but it seems to work:
q=0
While q < Me.Frame1.Controls.Count
If Me.Frame1.Controls(q).Name = "txtGroup7" Then
Me.Frame1.Controls.Remove q
Else
q = q + 1
End If
Wend
You must have something else wrong in your code, because Removeshould be working. Tried :
Private Sub ToggleButton1_Click()
If ToggleButton1.Value = True Then
Me.Frame1.Controls.Add "Forms.TextBox.1", "Text1", True
Else
Me.Frame1.Controls.Remove "Text1"
End If
End Sub
on a UserForm with a ToggleButton and a Frame and it correctly add and remove the TextBox when pressed.

How to loop through menu items

I'm a starting VB programmer, and I have been trying to loop through menu items, but this error always comes up:
Unable to cast object of type 'System.Windows.Forms.ToolStripMenuItem'
to type 'System.Windows.Forms.MenuItem'.
Here is my code
Function prop(ByVal objprop)
For Each mnuitem As MenuItem In ColorsToolStripMenuItem.DropDownItems
mnuitem.Checked = False
Next
Return Nothing
End Function
Excuse my poor programming, but can somebody help me get this piece of code working? Thanks!
Set the correct Type for mnuItem. You are using a System.Windows.Forms.MenuItem instead of the correct type, which is System.Windows.Forms.ToolStripMenuItem. As you can tell there are different kinds of MenuItems, so you must specify the correct Type. Also, since this code only returns Nothing it would be better suited as a Sub Routine.
Sub prop(ByVal objprop)
For Each mnuitem As System.Windows.Forms.ToolStripMenuItem In ColorsToolStripMenuItem.DropDownItems
mnuitem.Checked = False
Next
End Sub

Compile Error: Argument Not Optional | Access VBA

I have a form on Access where I have 3 ComboBoxes (Section, Rooms: White House, Rooms: Churchills). I need to have it so when 'White House' is selected from the 'Section' ComboBox, 'Rooms: Churchills will be Locked. And the opposite for 'Churchills'.
I have the code:
Private Sub Section_Change()
If Section.Value = "White House" Then
Rooms__White_House.Enabled = True
Rooms__Churchills.Enabled = False
Else: Section.Value = "Churchills"
Rooms__White_House.Enabled = False
Rooms__Churchills.Enabled = True
End If
End Sub
But when I test the form it throws the error:
Compile Error: Argument Not Optional
Any Help Appreciated. Thanks in Advance!
The problem is that Section is a reserved word. So the compiler doesn't recognise it as the name of your combobox. Just change the name of the combo to something else, e.g. cboSection, and your code will compile. (You naturally need to change your code to match the new name).
This link shows a list of names that you should avoid when working with Access http://allenbrowne.com/AppIssueBadWord.html
BTW, your line
Else: Section.Value = "Churchills"
seems a bit odd as it is setting the value of the combo. I'm guessing that you are meaning to comment what the value must be if you enter the Else section. If that's the case then you need to add the apostrophe in front of the comment, i.e.
Else: 'Section.Value = "Churchills"

Null Value for datagridview cell vb.net

How to get for null value in datagridview i did this in the cell validation event but it dosent seem to work. I want the user to add a new row and someone force him to give an ID or delete the row. What am i doing wrong. This is not a how to do question. This is a what is wrong Question. So right now it detects null but once i corrected the cell it still dosent allow me to get out of the row.
If DataGrid.CurrentCell.ColumnIndex = 0 Then
If IsDBNull(DataGrid.CurrentCell.Value) Then
Msgbox("Cannot be Null")
e.cancel = true
ElseIf Not IsDBNull(DataGrid.CurrentCell.Value) Then
e.cancel = False
End If
End If
So i tried this and it works for me . it simply uses e.formatedvalue. Current Cell Value is the cell value before and after edit while formatedvalue is what is being typed it . i guess i understand now so here is the coding
If grdDataGrid.CurrentCell.ColumnIndex = 2 Then
If e.FormattedValue = "" Or IsDBNull(e.FormattedValue) Then
MsgBox("Cannot be Null")
e.Cancel = True
Exit Sub
End If
End If
They are also different ways like adjusting your column properties to not allowed null but since the column properties inherit from the database i decided to use this.
I think this should be works ...
If DataGrid.CurrentCell.ColumnIndex = 0 Then
If IsDBNull(DataGrid.CurrentCell.Value) Then
Msgbox("Cannot be Null")
e.cancel = true
exit sub
End If
End If
Another way around this would be to explicitly set the default null value for the new datagrid cell to the value you are checking for. For instance,set the null= to an empty string if you are pulling string values. You can use the properties palette to set this.