Skip navigation
Currently Being Moderated

Useing a database.

Jul 20, 2011 6:36 AM

I have decided to write an examination using a database as the method for storing the questions and answers.

 

Seeing as I didn’t want to re-invent the wheel I created my database in Access making sure I could read it in Authorware.

 

I used the show me ODBC.a7p file as the base for my Authorware file and the way I retrieve the data.

 

All works fantastic and I can retrieve anything I look for however I am having just one small problem I hope someone can help me with.

 

For example I use    DB_SQLString:= "SELECT [ans4] FROM [Table] WHERE [recordnumber]=" ^ MyRecNum    to retrieve Answer no 4

 

I would like to use    DB_SQLString:= "SELECT [ans1], [ans2], [ans3], [ans4] FROM [Table] WHERE [recordnumber]=" ^ MyRecNum   to retrieve all answers, but unfortunately I then Can’t separate the resulting string into the separate answer elements..

 

I also hope this make sense…

 
Replies
  • Currently Being Moderated
    Jul 20, 2011 10:01 PM   in reply to The Pc Doctor

    You can use a loop to run through the resulting string and use GetLine

    and a custom separator to pull each item...thing is, hopefully the

    returned string has something separating each answer.

    Can you post a sample DB_SQLString result?

    Erik

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 21, 2011 9:22 PM   in reply to The Pc Doctor

    Can  you post a sample returned value for the DB_SQLString variable?

    Maybe the separator is there, just not easy to spot...?

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 22, 2011 4:49 AM   in reply to The Pc Doctor

    Use Trace(yourvariablename) to see what your variable looks like in the

    Trace window. You can select the text in the Trace window and right-click it

    to select Copy.

     

    Steve

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 25, 2011 3:31 AM   in reply to The Pc Doctor

    Your separator must be a RETURN character, which Authorware sees as "\r"

     

    You can use something like this to divide your data into a list, then

    it is easy to use

     

    Names:=[]

    repeat with i:= 1 to LineCount(yourdata)

        Names[i]:=GetLine(yourdata, i,i,"\r")

    end repeat

     

    Then when you need to get the 4th name, you use Names[4]

     

    Helps?

     

    Steve

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 25, 2011 8:38 AM   in reply to The Pc Doctor

    Looking at my code .. try changing :=1 to =1 ... Remove the colon. Does it

    work now?

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 25, 2011 9:06 AM   in reply to The Pc Doctor

    Scratch that, my first code was right. Just tested this:

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 25, 2011 9:18 AM   in reply to The Pc Doctor

    Forgive me if I'm missing something but to me it seems the issue is basic. You guys are forgetting the separators. Between records it's a return and between fields it's a tab.

     

    DB_SQLString:= "SELECT [ans1], [ans2], [ans3], [ans4] FROM [Table] WHERE [recordnumber]=" ^ MyRecNum

    DB_Data := ODBCExecute(handle, DB_SQLString)

    ResultLine = GetLine(DB_Data, 1) -- get first line (probably not necessary because there's only one

    Ans1 = GetLine(ResultLine, 1, 1, tab) -- get field 1

    Ans2 = GetLine(ResultLine, 2, 2, tab) -- get field 2

    Ans3 = GetLine(ResultLine, 3, 3, tab) -- get field 3

    Ans4 = GetLine(ResultLine, 4, 4, tab) -- get field 4

     

    This will get the items into the places you want. To take it a bit further you look at the code and notice that they're identical except for the number used for the Ans and the field. A common practice in this case is to set up a loop with a control variable that executes the same code over and over. Our problem is that we have four different variables and we can't cycle through the names to assign values. A "List" type variable is the best idea. We'll create a list type called Ans

     

    Ans = [] -- empty list

    Repeat with i := 1 to 4

       Ans[i] = GetLine(ResultLine, i, i, tab) -- get a single item into the spot in the list

    End Repeat

     

    i is the control variable and it increments by one each repeat of the loop. So it's 1 the first time and it does GetLine(ResultLine, 1, 1, tab) and puts that in Ans[1]. On the second loop it does GetLine(ResultLine, 2, 2, tab) and puts it into spot 2. So anywhere in your code where you're using Ans1 you could use Ans[1] instead. If you don't want to keep using ans1, 2, 3, and 4 you can use the first example without the loop and just assign the four items one on each line.

    Let's say just for argument's sake you don't want to use a list and you do want to use the four variables and a loop to load them. You might want to do that because you don't know in advance if you're going to have 4, 2, or 6 answers. I mentioned up above that we can't just cycle through the names. Actually we can, but it involves some tricky code. The EvalAssign function takes a piece of text and executes it as if it were programming code. Here is the example of the loop with i as the control variable again but this time it uses EvalAssign to build the variable name Ans1, Ans2, Ans3, and Ans4 on the fly.

     

    RepeatWith i := 1 to 4

        EvalAssign("Ans"^i^" = GetLine(ResultLine, "^i^", "^i^", tab)")

    End Repeat

     

    This is what we used to use before lists were made. This is a rather trivial example. In order to make it mroe real-world we'd also control the limit of 1 to 4 with some other variable to indicate how many distracters are being loaded.

     

    HTH,

    Mike

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 25, 2011 9:29 AM   in reply to Michael V Baker

    Eek - EvalAssign("Ans"i" = GetLine(ResultLine, "i", "i", tab)")

     

    What's wrong with a nice List?

     

    Apparently looking fot Return separator didn;t work for him, though

    that surprises me ...

     

    Steve

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 25, 2011 9:45 AM   in reply to Steve Howard, ACP

    I don't think it was a return separator the OP needed. It was the tab separator between fields of the return. Between each record will be a return, between the fields like [Ans1] and [Ans2] will be a tab char.

     

    Only thing wrong with using a list is if there's already 20,000 places where the code has Ans1, Ans2, etc on displays or other code.

     

    Mike

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points