Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VBA Probably a simple solution??!! Please help

Posted on 2003-11-13
15
Medium Priority
?
303 Views
Last Modified: 2010-05-03
Hi Folks

I am reasonably new to VB and as a project i am trying to create a game of pairs! So far i have randomly generated 16 images on a form. Each image is covered by a CommandButton. When the button is clicked .visible = false and so the picture is revealed!

Now i am stuck! i don't know the code which I need to check to see if the two visible images on the screen are the same.

Just to confirm what i need, something like this

userform.visible.images = same then....!! (i know the syntax to the left is incorrect but i wanted to demo how i wanted to go about it)

Does anyone have any idea's?? Please

0
Comment
Question by:craigpike
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 3
  • +2
15 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 9740090
What are you storing your picutres in (image control, picturebox, image list...), and how are you randomly displaying them?
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 9740138
rather than trying to compare the images, you need to think a bit more ABSTRACTLY.  The images are simply a representation of the item, to be compared.  Let's say for instance, that you have a 4 x 4 grid of items.  This is 16 total, or 8 pairs, represented by 8 pairs of images.  But the 8 pairs could just as easily be 8 pairs of NUMBERS, and still be REPRESENTED by the 8 pairs of images.

so a 2 dimensional array  Items(4,4) to hold the 8 pairs   now each button corresponds to one cell in the array, and you simply compare the values associated with the two buttons, so see if the SAME number is in both cells.  The IMAGE that is displayed on the screen is associated with the NUMBER in the corresponding cell in the array.

so you have 8 numbers (1,2,3,4,5,6,7,8), and 8 Images (img1,img2,img3,img4,img5,img6,img7,img8)

you place the 8 NUMBERS randomly in the array, so that you have 2 occurences on any one NUMBER, and then using that array, place the images in the corresponding positions on the screen. so if the NUMBER at Items(1,1) is 8, then the image placed in the UPPER LEFT corner of the screen would be img8, under the Button#1 - I would also use a Control array for the Buttons (so that ALL of the Buttons have the same NAME, but are indexed 0 to 15 - thus button(0) corresponds to Items(1,1), button(3) corresponds to Items(1,4), and button(15) coresponds to Items(4,4))

AW

AW

0
 
LVL 1

Accepted Solution

by:
akari2000 earned 800 total points
ID: 9740251
Just give each image its own number. Then check on number

like

image1 = 1
image2 = 2

if image1 = image 2 then

code

end if

something like that
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:craigpike
ID: 9740281
I am actually doing this project in works time so i am using VBA which use imagebox i believe.

The images are 100% random, as you can see below they are not tagged in any way. The code that i have used to generate the images is probably very inefficient, but it works!

10  X1 = Int((8 * Rnd) + 1) ' Fills image frame A with rand pic!
    PicString = "c:\pix\pic" & X1 & ".jpg"
    A.Picture = LoadPicture(PicString)

20  X2 = Int((8 * Rnd) + 1) ' Fills image frame B with rand pic!
        Select Case X2
            Case Is = X1
                GoTo 20
            End Select
    PicString = "c:\pix\pic" & X2 & ".jpg"
    B.Picture = LoadPicture(PicString)

etc....
0
 
LVL 1

Expert Comment

by:InTheWind
ID: 9740828
You have....

10  X1 = Int((8 * Rnd) + 1) ' Fills image frame A with rand pic!
    PicString = "c:\pix\pic" & X1 & ".jpg"
    A.Picture = LoadPicture(PicString)

if you added...

A.tag = PicString
.
.
.
B.tag = PicString

Then you just have to compare the .tag properties of the two pictures in a simple "if ... = ... then"
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 9741064
so do I understand that you are picked ALL of the images randomly?  and howmany images are possible.  What do you do if there are THREE instances of the same image (this is a possibility with what you have now).  What do you do if there is NO pair (that is an Image that DOES NOT have another copy)?

what you should do is this:

1) set up a single dimensioned array (Vals(8)) to hold the values 1 through 8

then scramble this array (put the 8 values in random order) this is also called SHUUFLING (as in shuffling a deck of cards - which scrambles the 52 cards into a random order - you just have 8 cards to put into a random order)

that way you are sure that you will use ALL 8 images, and then you can also be sure that EVERY image is present EXACTLT twice - hence EVERY image will make a PAIR.

now you go through the Vals array one at a time, and place each value in two distinct locations in the Items array (or place each image in your ImageBox).

This approach will make your overall programming problem MUCH easier to deal with.

AW

0
 
LVL 1

Expert Comment

by:InTheWind
ID: 9741300
if you used an "image list" to do what AW is recommending then you would make this simpler.

I would think, in the case of an image list, all you need to do is...

for i = 0 to x (number of image controls)
  select an image (doing that in order would be fine)
  do while j <= 2
    randomly pick an image control
      if there is no image designated
        load image
        increment j
      else
        randomly pick an image control
   loop
next

doing this until you have loaded all the images will allow you to control the amount of time you spend randomly picking image controls to load a picture into. picking more images than you have image controls means that you would only do the first loop for the number of times that you have image controls to handle images, in reverse you might need to kick out of the for if you have reached that point.

hope that works for you.
0
 

Author Comment

by:craigpike
ID: 9741973
Thanks every one for your help, they are all great answers. I have learn't a lot from reading your articles.I am gonna have to digest some of these principles so I will be rewarding the points soon.

Thanks again, they are all very very helpfull so I will increase the points to 200

cheers
0
 
LVL 1

Expert Comment

by:InTheWind
ID: 9742144
yeah, but who gets em? or are you going to give "assisted" solutions?

good luck
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 9742456
InTheWind, if you take ALL of the points and about $4.50 US, then you can go to Starbucks and get a Medium Mocha Latte!

If it's that important to you, then you can have mine, becuase I ca't use them for anything 'meaningful'

AW
0
 
LVL 1

Expert Comment

by:InTheWind
ID: 9742645
haha

you're so right... and you need them so badly so you would feel that way of course! ;-)

Cheers... I mean, slurp.
0
 

Author Comment

by:craigpike
ID: 9759529
Sorry, I have been STUPID and allocated the points to the wrong person. No offence akari2000 but i ment to give the points yo AW. Does any one know if this can be rectified??!!

Sorry AW
0
 
LVL 1

Expert Comment

by:InTheWind
ID: 9759755
he said he didn't care about the points, so I wouldn't worry about it.
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 9759932
i appreciate the fact that I *should* hae gotten the points (though, as I indicated, they aren't worth much, so no big deal).

After all, with all those points, can I retire?  Can I buy a new car? Can I even buy a small bag of French Fries at McDonald's .  I think not.  Bit it si the appreciation of 'peers' that IS a big deal.

AW
0
 
LVL 1

Expert Comment

by:InTheWind
ID: 9760402
exactly and I thought he was being very nice by offering the points to someone... I thought that your reply to me was a little bit harsh considering his kind offer to increase the points. I was just trying to make the point that he had to actually make the offer into an "accepted answer"...

Enjoy your new "digital car" ;-)

TTFN
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question