Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

click a label, pass some code... 1

Status
Not open for further replies.

AJLoehr

Programmer
Oct 3, 2008
26
US
I'm trying to make an access form to simulate a Sudoku puzzle. So if you know Sudoku and you understand what a "Candidate" is... you'll know there are 729 candidates on a standard Sudoku puzzle. So I have 729 labels.

I want to be able to single click a label and set the label.caption to the appropriate candidate number. Or double click it and clear the label.

Currently I have the first 2 block of 9 candidates working... but it's going to be way too much code. So please help me reduce it.

My pseudo code would be...

Code:
[green]'all my candidate labels are named "Box###" and the last number is the candidate number.  So Box122 would be the first set of 9, second block, candidate position 2.[/green]

If any label on the form is clicked...
    If the label.name starts with "B"  [green] 'because there are other labels I don't want to apply this code to [/green]
         Set label.caption = last character of the label.name


If any label on the form is double-clicked...
    Set label.caption = ""   


[green] 'oh and if you were curious, currently I've got it coded like this...[/green]


Private Sub Box111_Click()
    Call passbox("Box111")
End Sub

Private Sub Box111_DblClick(Cancel As Integer)
    Call clearbox("Box111")
End Sub

Private Sub Box112_Click()
    Call passbox("Box112")
End Sub

Private Sub Box112_DblClick(Cancel As Integer)
    Call clearbox("Box112")
End Sub

Private Sub Box113_Click()
    Call passbox("Box113")
End Sub

Private Sub Box113_DblClick(Cancel As Integer)
    Call clearbox("Box113")
End Sub

[green] Etc....[/green]


Sub passbox(xx As String)
    With Me.Controls(xx)
        .Caption = Right(xx, 1)
    End With
End Sub

Sub clearbox(xx As String)
    With Me.Controls(xx)
        .Caption = ""
    End With
End Sub


Like I said, the above code is working... but I don't want to repeat it 729 total times... so any help on this would be appreciated. Thanks in advance,

AJ
 
Whoa! 729! For display purposes you only need 81 controls for display and if you want to display possibles you can either add another 81 under, next to, etc. or use some sort of bolding parens or whatever to distinguish between possibilities and locked in hints and guesses. Check out for some examples.

Good Luck
 
Hey I'm new here and you are probably an uber coder... but I know Sudoku... so I don't mean any disrespect... but

A standard puzzle has 9 squares of 9 which is 81 and you can have 9 candidates in each of the 81 squares. Which means 729 little squares. Trust me I'm looking at them on my form.

So please... I'm asking for help coding, not counting my little squares. Again, I don't mean disrespect
 
I did not take it that you were being disrespectful but let me try to explain what I was trying to say.

you have a grid of 9x9=81 squares (or controls whether they be labels, text boxes, or some other control), which contain nothing at the start. (my unfinished version)

Then you add in some such way the hints that are given to solve the puzzle.

(Now this is where we may have come across some communication confusion)

From there, based upon the rules of sudoku, the other controls, that do not contain the starting hints, are left with possible "candidates" (hence my confusion about your post) for solving the puzzle. Now whether these other controls that do not contain the starting hints actually display the possible "candidates" or another control to the bottom, left, right, top, as in a string of numbers that you can assemble and parse in code.

example (sort of) (each line represents a line of a puzzle)
Code:
A5B   C3D   E4F
4GH   IJ2   6KL
MNO   P1Q   R5S

A=1-2-6-7-8-9 (less any other numbers in the verticle) as possible "candidate" numbers for solving the puzzle.

so you see my confusion at the use of the word "candidate".

So I now understand that you are using for each "cell"...

A cell being defined as an area of some sorts that represents the hint number, guess number, or solved number of a sudoku puzzle.

...9 controls

So, what I was trying to point out is that for each cell you only need one control. This control (whatever it is) can be used to not only display the hint, guess, or solved value of the sudoku puzzle (say in bold with a larger font), it can also be used to display the available possibilities for that cell (say in a non bolded smaller font). This will help you reduce your code dramatically!

Sorry for the confusion.

Once again please visit PSC and search on sudoku. There are many examples (most automatically solve the puzzle) or search the ms office sudoku solvers (you will also find creators and puzzles of the day listings).

So that is my present hint on how to reduce your code.

Good Luck
 
Ok, I see what you are saying here:

So, what I was trying to point out is that for each cell you only need one control. This control (whatever it is) can be used to not only display the hint, guess, or solved value of the sudoku puzzle (say in bold with a larger font), it can also be used to display the available possibilities for that cell (say in a non bolded smaller font). This will help you reduce your code dramatically!


But then I would have to put the candidates as

1,3,5 and not in the same checkerboard pattern as the larger blocks.

Code:
\/\/\/\/\/  Below is one block of 9 numbers showing the possible candidates   \/\/\/\/\/\/

[1][2][3][green][b]|[/b][/green][1][2][3][green][b]|[/b][/green][1][2][3]
[4][5][6][green][b]|[/b][/green][4][5][6][green][b]|[/b][/green][4][5][6]
[7][8][9][green][b]|[/b][/green][7][8][9][green][b]|[/b][/green][7][8][9]
[green][b]-----------------------------[/b][/green]
[1][2][3][green][b]|[/b][/green][1][2][3][green][b]|[/b][/green][1][2][3]
[4][5][6][green][b]|[/b][/green][4][5][6][green][b]|[/b][/green][4][5][6]
[7][8][9][green][b]|[/b][/green][7][8][9][green][b]|[/b][/green][7][8][9]
[green][b]-----------------------------[/b][/green]
[1][2][3][green][b]|[/b][/green][1][2][3][green][b]|[/b][/green][1][2][3]
[4][5][6][green][b]|[/b][/green][4][5][6][green][b]|[/b][/green][4][5][6]
[7][8][9][green][b]|[/b][/green][7][8][9][green][b]|[/b][/green][7][8][9]

Hopefully that explains it better as to what I'm trying to accomplish. Yeah you are right, if all I wanted was a 1 line of possible candidates, it wouldn't take 729 labels... but I have a distinct picture in my mind of how this is to look and how the program will operate, so I need the 729 labels.

Oh and I'm not trying to make a program to solve the sudoku puzzles for me. That wouldn't be any fun. I'm making a program that will allow me to input the starting candidates from my books I buy... then work the puzzle on my program. Then transfer the answer to the book. Sudoku books are made from the worst recycled paper ever and erasing on them (well at least as many times as I erase on them) leads to erasing thru the paper in some cases or erasing the numbers/lines that are printed in the book and not just my candidates. So I decided to make my own program to help me.

Anyway, thanks for responding and I'll check out the link you gave me.

AJ
 

Okay, ... I under...stand. Now how about using the mouse down event?
Code:
Option Compare Database

Private Sub Form_Load()
Me.ShortcutMenu = False
End Sub

Private Sub Label2_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Display Button, Label2
End Sub

Private Sub Display(Button As Integer, C As Label)
If Button = 1 Then
    C.Caption = Right(C.Name, 1)
ElseIf Button = 2 Then
    C.Caption = ""
End If
End Sub
That should reduce the amount of code by half and instead of constantly double clicking, you just need to click either the left or right mouse buttons.

How's that for reducing code?

Good Luck

 
I don't quite understand what I'm supposed to do with that.

I'm a novice to coding but creating a sub "Display" is not really doing anything. "Button" is just a placeholder, but the code is written as if VBA is supposed to know that it's a mousebutton. I checked the object browser and "button" isn't a known object.

Anyway, I threw it into the code for the form and it didn't work. Maybe I'm missing something.

Currently I'm using:

Code:
Sub passbox(xx As String)
    With Me.Controls(xx)
        Select Case Me.Controls(xx).Caption
            Case ""
                .Caption = Right(xx, 1)
            Case Else
                .Caption = ""
        End Select
    End With
End Sub

to set the candidate...

But I have to set code to each labels click_event (all 729) to

Code:
Private Sub Box111_Click()
    Call passbox("Box111")
End Sub
 
One other question... is there something wrong with passing a number 9. I use the code above for all the labels, but if the middle number of the label is "9", my program crashes... and I don't mean, gets an error... I mean crashes completely, shuts off and closes Microsoft Access.

Box129 works fine
Box159 works fine
Box19# crashes....

And I've cut and pasted all the objects in the form and changed the names manually. I've rechecked all the names and they match the code appropriately...

I've deleted and recreated all the objects in questions twice now. I've also changed the event to activate a different label.... cause I was curious if it was the click_event or the passing of the number. And it seems to be the passing of the "9".

If you want to look at the file, I've uploaded it to my skydrive acct on Windows Live.

My acct there is attached to my email....
snayjay@hotmail.com
Just goto Skydrive or Microsoft Live, if you aren't a current user. The program is in my Public folder and is called SudokuBandAid.

Any help would be greatly appreciated.
 
Okay, look at my code more closely. I use the MouseDown event and not the click or dblclick events.

Start a new form in your database or create a test database and add a form. Then add a few labels (Label1, Label2, Label197). You can set the name of these labels in the properties dialog on either the other tab or the all tab.

Then, for each control go to the event tab. Go to the On Mouse Down drop box and select [Event Procedure]. Then click on the ... button. This will take you to the code window.

Now, above you are two combo boxes (drop down lists) right above the code pane. On the left one, drop the list and select one of the other labels. This will create the default procedure for that label and that is the click event. Now on the right drop down combo select the mousedown event. (Then delete the click event)

Code:
Option Compare Database

Private Sub Form_Load()
Me.ShortcutMenu = False
End Sub

Private Sub Label1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Display Button, Label1
End Sub

Private Sub Label197_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Display Button, Label197
End Sub

Private Sub Label2_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Display Button, Label2
End Sub

Private Sub Display(Button As Integer, C As Label)
If Button = 1 Then
    C.Caption = Right(C.Name, 1)
ElseIf Button = 2 Then
    C.Caption = ""
End If
End Sub

Copy the Display sub as is. Now let me explain the mousedown event for you...

Button is an integer that represents which button of the mouse has been pushed down

Shift is another argument passed to this procedure but it represents keyboard combinations of the Shift, Ctrl, and Alt keys

X and Y are from the left and from the top positions of the control which represent where on the label the mouse was clicked.

As you can see we don't use anything but the button arguement. Now I will explain the Display procedure for you.

It accepts two arguements Button As Integer (just like the mouse down event) and C as Label. So when we call the Display sub with...
Code:
Display Button, Label2
what we are doing is passing the button arguement that was passed to the mousedown event to the Display prodedure where we evaluate its value. As for the Label2 arguement that we are passing to the C As Label, we are passing a reference of the Label2 control. That is why and how we can reference the label directly via the C.Caption and C.Name you see in the Display sub.



Now as to why access is crashing... Don't know, I will try to get your db or you could paste a url to it in your post

Good Luck

 
Alright.... I understand your code now, my problem was I assumed Access automatically added the Event Procedure to the Mousedown property of the label, but it didn't. So the code was there, but the mousedown event property of the actual label didn't point to the code. Fixed now. That does work... and it would reduce the code some with some minor fixes... so thanks very much for that.

However, I know there has to be a way to follow my pseudo code more closely which would do more than cut the 729 subs of code that are required even with your method. Does anyone know a way to code it more like my pseudo code. (I've changed it from the original post slightly as I've realized a way to use single-click for both setting and erasing the candidate... then using double-click for setting and erasing the answer. You could do the same for button 1 or 2 if you wanted though).

Pseudo Code:

Code:
If any label is clicked then
   If left(label.name) = "B" then
       select case label.caption
           case ""   [green]'not set[/green]
               label.caption = right(label.caption,1)
           case else  [green]'set to a number[/green]
               label.caption = ""
       end select
   end if
end if

That would set the candidates for all my 729 labels with 1 sub without having to add a line of code to every label.mousedown or click event. I thought maybe a private class could be made to distinquish an accessobject(label) being clicked, but I'm not that experienced yet. If someone can help me with that, I'm sure I could figure out to do the rest. Then I would have only 2 subs vs more than 700.

I also added a link to the file. Hope that works... it's placed online at Skydrive.Live.com (snayjay@hotmail.com)

Thanks again.

AJ
 
 http://cid-34b5cd6e0f521802.skydrive.live.com/self.aspx/.Public/SudokuBandAid.accdb
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top