Forum Home
Press F1
 
Thread ID: 68523 2006-05-02 02:04:00 VB code in Access pico (4752) Press F1
Post ID Timestamp Content User
451324 2006-05-02 02:04:00 Hi all,

I am trying to write some code to check to see whether a list box control has a value selected or not. I have some other controls on the form which modify what is displayed in the list box so sometimes there is no value selected. I want to either check to see if there is a value selected or diable a button that goes to the next form because if there is no value selected there is a problem.


Private Sub ViewProject_Click()
Dim Check

Check = CheckHasValue(ProjectSelectWindow)

Select Case Check
Case "Error"
MsgBox "Error"
Exit Sub
Case False
MsgBox "Please select a project first."
Exit Sub
Case True
CurrentProject = Form_ProjSelect.ProjectSelectWindow
DoCmd.OpenForm "Proj"
DoCmd.Close acForm, "ProjSelect"
End Select

End Sub

This is what I have so far. It calls a user-defined function, CheckHasValue which checks to see if the control has a value. This is the code for that function.


Public Function CheckHasValue(ObjectToCheck As Object)

If ObjectToCheck = Null Then
CheckHasValue = False
ElseIf ObjectToCheck <> Null Then
CheckHasValue = True
Else
CheckHasValue = "error"
End If

End Function

I'm not sure where the problem is occuring but the function seems to be returning "error" when it is called.

Any suggestions?
pico (4752)
451325 2006-05-02 02:32:00 Just quickly looking at your code it appears that you are passing the name of the form rather than the name on the control that you want to check (e.g. textbox). Try passing the control name and you should hopefully have more luck! odyssey (4613)
451326 2006-05-02 02:50:00 Thanks Odyssy,

The control name is "ProjectSelectWindow". The problem is the function is testing ProjectSelectWindow to first see if it is null then not null and else return "error" which it seems to be doing. Should I not be referring to the ProjectSelectWindow as an object?
pico (4752)
451327 2006-05-02 04:09:00 Hi Pico

I think you need:

Check = CheckHasValue(ProjectSelectWindow.Text)
and:

Public Function CheckHasValue(ObjectToCheck As String)

If ObjectToCheck = "" Then
CheckHasValue = False
ElseIf ObjectToCheck <> "" Then
CheckHasValue = True
Else
CheckHasValue = "error"
End If

End Function

This will send the text value from the listbox to the CheckHasValue function as a string, which requires a slight modification in that function to check for an empty string rather than checking for an object.
Antmannz (6583)
451328 2006-05-02 22:22:00 Hi again Antmannz,

Hey I tried your solution and firstly it errors saying "Method or data member not found". Referring to the .text. So I changed it to


Check = CheckHasValue(ProjectSelectWindow.Value)

Hoping it would then return the value in the listbox as a string. Then it errors saying "invalid use of null". Stopping at the same point. However when there is a value selected in the list box everything works fine. So the problem is occuring when trying to pass a null value to the function, is that what would cause an error?
pico (4752)
451329 2006-05-03 01:56:00 Looked at your code a bit harder this time! Basically you should be using the "isnull" function to check the list box. I'm assuming that you are limiting the list box to a know list. You should change your CheckHasValue function to be:


Public Function CheckHasValue(ObjectToCheck As Object)

CheckHasValue = False
If IsNull(ObjectToCheck) = False Then CheckHasValue = True

End Function

And update your Case "Error" to be Case Else and the last item in your select statement.
odyssey (4613)
451330 2006-05-03 03:18:00 Candidly I haven't really looked over your code, but your function could use "err.clear: on error resume next" and check for error codes. I try to avoid VB in Access (or Access at all), so I could be wrong. kingdragonfly (309)
1