Specific Values for Textbox Input

accessNOOB1234

New member
Local time
Today, 15:03
Joined
Jun 26, 2011
Messages
3
I have VB Code running (which I pulled from another site), and I was wondering if there was anyway to make it say that ONLY these entries are valid.
Code:
DoCmd.SetWarnings False
If IsNull(DLookup("ProgramName", "tbl123", "ProgramName ='" & Me.ProgramName & "'")) Then
DoCmd.RunSQL "INSERT INTO tbl123(ProgramName, Status) VALUES('" & Me.ProgramName & "', '" & Me.Status & "')"
Else
DoCmd.RunSQL "UPDATE tbl123 SET Status ='" & Me.Status & "' WHERE ProgramName='" & Me.ProgramName & "'"
End If
DoCmd.SetWarnings True
I was wanting to only allow the status to be set to Active or Inactive. Is there a way to do this?
 
Personally I'd probably use a combo box with its Limit to List property set to Yes. It would get the two values from a table, which would allow you to add a status later. You could probably also use a validation rule or code like:

Code:
If Me.Status = "Active" Or Me.Status = "Inactive" Then
  'do your thing
Else
  Msgbox "Invalid status"
End If
 
I agree with Paul, when limiting an entered Value is necessary, a Combobox is always a good approach. But if you insist on a 'freehand' data entry approach, this will work:
Code:
Private Sub Status_BeforeUpdate(Cancel As Integer)
 If Me.Status = "Active" Or Me.Status = "Inactive" Then
  'do whatever or simply do nothing
Else
  MsgBox "Invalid Status"
  Cancel = True
  Status.SelStart = 0
  Status.SelLength = Len(Me.Status.Text)
End If
End Sub
Linq ;0)>
 
missinglinq- When I run that code, I get an error of "Compile Error: Variable Not Defined" on this portion of code:

Code:
Cancel = True
 
What event are you running it in? It is intended for the before update event.
 
I was trying to run it in a onclick() event. I was trying to add it to some simple VB code that I had found...

Code:
'DoCmd.SetWarnings False
'If Me.Status = "Active" Or Me.Status = "Inactive" Then
'Else
'MsgBox "Invalid Status"
'Cancel = True
'Status.SelStart = 0
'Status.SelLength = Len(Me.Status.Text)
'End If
'If IsNull(DLookup("ProgramName", "tbl123", "ProgramName ='" & Me.ProgramName & "'")) Then
'DoCmd.RunSQL "INSERT INTO tbl123(ProgramName, Status) VALUES('" & Me.ProgramName & "', '" & Me.Status & "')"
'Else
'DoCmd.RunSQL "UPDATE tbl123 SET Status ='" & Me.Status & "' WHERE ProgramName='" & Me.ProgramName & "'"
'End If
'DoCmd.SetWarnings True
 
Again, Linq's code is only appropriate for the before update event. Did you try incorporating yours into what I posted?
 

Users who are viewing this thread

Back
Top Bottom