Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    rwahdan1978 is offline Advanced Beginner
    Windows 11 Access 2016
    Join Date
    Jun 2024
    Posts
    57

    hangman game using spaces

    Hi

    I have a procedure that will have dashes where the letters will be presented after the user guess a letter. In my table I have words that has spaces such as "school time". In the box I will have "-----------" so how to replace that bold dash with a space instead?



    here is my procedure (wordhiidens holds the name "school time":
    Code:
    Private Sub DisplyWordShown()
    
    
        Dim X As Long, S As String
        WordShown = ""
        
        For X = 1 To Len(WordHidden)
            S = Mid(WordHidden, X, 1)
            If InStr(LettersGuessed, S) <> 0 Then
                WordShown = WordShown & S & " "
            Else
                WordShown = WordShown & "_ "
            End If
        Next
        
    End Sub

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,551
    Why not put that dash in the words in your table?
    Else Google 'replace vba'
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Why is there a bold dash to begin with?

    Are the letter positions shown by a dash or an underscore?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    rwahdan1978 is offline Advanced Beginner
    Windows 11 Access 2016
    Join Date
    Jun 2024
    Posts
    57
    Quote Originally Posted by June7 View Post
    Why is there a bold dash to begin with?

    Are the letter positions shown by a dash or an underscore?
    dashed. I need to know how to capture a space (from a user) and place it there in the textbox (replace the - with space).

  5. #5
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,421
    I'd say it's to point out that there are 2 words (or more as the case may be). Replace function, as suggested, should work.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    rwahdan1978 is offline Advanced Beginner
    Windows 11 Access 2016
    Join Date
    Jun 2024
    Posts
    57
    Let me make it easier,

    If i have 2 words in a table field say "First Name" then space then "LastName" so the name is "Rami Wahdan" so I want the function to do this:
    "---- ------", so how to do that?

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,551
    Use replace() and the alphabet?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  8. #8
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,421
    Maybe something like (untested)
    Code:
    Sub MakeDashes
    Dim str As String
    Dim i as Integer, n As Integer
    
    str = [First Name] 
    n = Instr(str," ")
    For i = 1 to Len(str)
        str = Replace(Mid(str,i,1),"-")
    Next
    str = Replace(n,str," ")
    
    End Sub
    EDIT -
    Not very robust I know, even if it works. It does not check for the existence of any space(s); just going by what info you provided. You may need it to be a function as well.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Micron, it does not work. For one thing, arguments used for Replace() are not correct.

    Could there be other characters such as apostrophe as in "Pete's Dog"? Would you want them to display? Since names can have dash (hyphen), use underscore to mask string. To set up the initial pattern replacing only letters and displaying any punctuation/spaces, consider:
    Code:
    Dim strW1 As String, strW2 As String, strL As String, x As Integer
    strW1 = "Rami Wahden"
    For x = 1 To Len(strW1)
        strL = Mid(strW1, x, 1)
        strW2 = strW2 & IIf(strL Like "[A-z]", "_", strL)
    Next
    Me.Text11 = strW2
    Replacing appropriate dashes with guessed letter gets complicated. Replace() won't work for that.

    This has been done before https://www.bing.com/videos/rivervie...69&FORM=VRDGAR
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  10. #10
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,551
    I would probably just replace alphabetical characters with underscore? then you do not need to worry about spaces and ', even hyphens?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  11. #11
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,421
    Hi, yea saw that within a few minutes. I found trying to replace just one character in the middle-ish of a string to be problematic so I went with breaking the string into its parts
    Code:
    Function MakeDashes(str As String)
    Dim i As Integer, n As Integer
    Dim str1 As String, str2 As String
    
    n = InStr(str, " ") + 1
    str1 = Left(str, n - 2)
    str2 = Mid(str, n)
    Debug.Print str1
    For i = 1 To Len(str1)
        str1 = Replace(str1, Mid(str1, i, 1), "-")
    Next
    For i = 1 To Len(str2)
        str2 = Replace(str2, Mid(str2, i, 1), "-")
    Next
    
    str = str1 & " " & str2
    Debug.Print str
    
    End Function
    RegExp is almost always neater. I just never learned it (too old now to be bothered, I think).
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  12. #12
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,551
    I would not even split it.
    What if you had Hugh Fearnley-Whittingstall ?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  13. #13
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Right, I would use underscore instead of dash for masking (revised my earlier post).

    Micron, is your code supposed to replace dash with guessed letter?

    Just setting up the initial masked string isn't that complicated.

    For RegExp, activate "Microsoft VBScript Regular Expressions 5.5" library or use late binding.

    Never too old (70+ and still learning).
    Last edited by June7; 08-03-2024 at 04:12 PM.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  14. #14
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,551
    Code:
    Function HangMan(strAnswer As String) As String
    Dim iChar As Integer
    
    
    For iChar = 65 To 90
        strAnswer = Replace(strAnswer, Chr(iChar), "_")
    Next
    HangMan = strAnswer
    
    
    End Function
    Code:
    ? hangman("Hugh Fearnley-Whittingstall")
    ____ ________-_____________
    or even better, make the substitution "_ " then you get

    Code:
    ? hangman("Hugh Fearnley-Whittingstall")
    _ _ _ _  _ _ _ _ _ _ _ _ -_ _ _ _ _ _ _ _ _ _ _ _ _
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  15. #15
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,551
    Quote Originally Posted by rwahdan1978 View Post
    dashed. I need to know how to capture a space (from a user) and place it there in the textbox (replace the - with space).
    I would have one textbox for the question, another for the answer, and a label for a clue(s)
    So I would key in

    hugh fearnley-whittingstall and you would compare to the original value?

    The spaces between the underscores are merely so the user can see how many characters are required?

    You could update the question control as they get correct characters.

    Bit of jiggery pokery though.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 04-26-2020, 02:01 AM
  2. First game of the season
    By Bengtsson in forum Queries
    Replies: 5
    Last Post: 03-16-2020, 01:26 PM
  3. Math Game
    By pkstormy in forum Code Repository
    Replies: 0
    Last Post: 12-26-2019, 05:34 PM
  4. Play Hangman in MSAccess
    By pkstormy in forum Code Repository
    Replies: 8
    Last Post: 12-26-2019, 04:23 PM
  5. Bingo Game
    By Gus in forum Access
    Replies: 0
    Last Post: 12-14-2008, 03:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums