You are on page 1of 3

Voting system using Microsoft Access

Asked 6 years, 3 months ago Modified 6 years, 3 months ago Viewed 2k times

thanks for passing by.

I just have two questions. I am on college and I need a bit of help.


-2
I need to do a voting system using Microsoft Access, I have two problems:

1) I don't know how to "Add 1" to the field 2) I can't find a way to avoid the user from selecting
more than one candidate (Using checkboxes)

This is what I have so far:

Private Sub Comando25_Click()


Dim Check1 As Boolean
Dim Check2 As Boolean
Dim Check3 As Boolean
Dim Check4 As Boolean

If (Me.Check1 = True & Me.Check2 = True & Me.Check3 = True & Me.Check4 = True) Then
MsgBox "You can't select them all, you can only choose one"
DoCmd.Close
DoCmd.OpenForm "Ir a"
End If

If (Me.Check1 = True & Me.Check2 = True & Me.Check3) = True Then


MsgBox "You can't select three, you can only choose one"
DoCmd.Close
DoCmd.OpenForm "Ir a"
End If

If (Me.Check1 = True & Me.Check2 = True) Then


MsgBox "You can't select two, you can only choose one"
DoCmd.Close
DoCmd.OpenForm "Ir a"
End If

If (Me.Check2 = True & Me.Check3 = True) Then


MsgBox "You can't select two, you can only choose one"
DoCmd.Close
DoCmd.OpenForm "Ir a"
End If

If (Me.Check1 = True & Me.Check3) = True Then


MsgBox "You can't select two, you can only choose one"
DoCmd.Close
DoCmd.OpenForm "Ir a"
End If
End Sub

This is the form: Form


This is the table, I have to add one to the "Votos" part. Table

I hope you can help me out

database ms-access

Share Follow asked Oct 23, 2017 at 13:18


Shredder
105 1 4

Report this ad

1 Answer Sorted by: Highest score (default)

Your If statements are problematic. You are not properly building your logical statements. &
concatenates two strings together. AND is a logical operator that links two conditions to the

1 overall evaluation of a statement.

Checkboxes have a unique characteristic that lets IF Me.Check1 = True Then mean the same
thing as If Me.Check1 Then . That's because the reference Me.Check1 will evaluate to either a
True or a False, exactly the parameter expected by the If statement.

The problem is, you cannot just use & to logically compare multiple tests. Your first If
statement should read:

IF(Me.Check1 = True AND Me.Check2 = True AND Me.Check3 = True) Then or the simpler
If(Me.Check1 AND Me.Check2 AND Me.Check3) Then where the evaluation of each reference will
result in either True or False and the AND operator combines them together to evaluate the entire
statement to a True or False.
Now that should work.

But I have to ask, do you really want to wait until the button is pressed to give feedback to the
user? It's even simpler to build a toggling routine on the checkboxes:

In the AfterUpdate event of Checkbox 1, place:

If Me.Check1 Then
Me.Check2 = False
Me.Check3 = False
Me.Check4 = False
End If

Now adjust that code for each of the other three checkboxes, first checking whether that
checkbox is true, and if so, all others must be false.

This approach resets all checkboxes any time the user checks any checkbox. That way, you don't
need to validate their selections during submission, making your code much easier to manage.

As for updating the vote tally, you will need two pieces of information and a sql UPDATE
statement. The two pieces of info you need are: a unique way to identify which row in your table
needs another vote and the current vote tally for that candidate/row. Do you know how to get
that information? Do you know how to create and run an Update statement that will work
regardless of who is being voted for? Hint: I would suggest using either a recordset or a query
definition object, please take a stab at updating a record using one of those two methods, there
should be tons of resources for both online.

Share Follow answered Oct 23, 2017 at 15:22


MoondogsMaDawg
1,714 12 22

Thanks I fixed the issue. – Shredder Oct 24, 2017 at 14:14

Although, my update statement doesn't work (It says sub doesn't exist): This is what I have If Me.Check1 =
True Then Update Candidatos Set Votos = Votos + 1 WHERE [Nombre] = Marco End If – Shredder Oct 24,
2017 at 14:31

Your update is invalid and is not being called correctly. What is "Macro"? And you cannot use Votos = Votos
+ 1. Try researching how to execute an update sql statement with vba. – MoondogsMaDawg Oct 24, 2017
at 16:09

You might also like