Results 1 to 4 of 4

Need Help with Update Statement

Threaded View

  1. #1
    Forum Contributor
    Join Date
    06-30-2008
    Location
    PSL, FL
    Posts
    271

    Need Help with Update Statement

    Hello guys,

    I am looking for a bit more help for my Data Entry form for Trivia Questions.

    I have now an almost completed form. I can create new records, I can scroll between records.

    PROBLEM IS

    DAMMM I can not update. the current record.

    I want to be able to press the SaveQuestion button and it to save the data as it is in the sheet.

    I also have two other small issues

    1) When I scroll between records, I want when I reach the top or bottom of the available records for it to circle.
    The way it currently is written when I spin button up past row 1 it crashes and gives me an error. I want once Row 2 (NOT 1 because that is an header) for it to go to the last row.

    2) I do not want to be able to click more than 1 of the 4 checkboxes at anytime


    Below is my code and I have attached my Workbook.

    Thanks for any help

    Matt

    Private Sub CBCat_Change()
    
    End Sub
    
    Private Sub CBTrvAns1_Click()
    
    End Sub
    
    Private Sub Label2_Click()
    
    End Sub
    
    Private Sub NewQues_Click()
    Dim iRow As Long
    Dim iaRow As Long
    Dim ws As Worksheet
    Dim ans As Worksheet
    Set ws = Worksheets("Questions")
    Set ans = Worksheets("Answers")
    
    
    'find first empty row in database
      iRow = ws.Cells.Find(What:="*", _
                             SearchOrder:=xlRows, _
                             SearchDirection:=xlPrevious, _
                             LookIn:=xlValues).Row + 1
                             
    'find first empty row in database
      iaRow = ans.Cells.Find(What:="*", _
                             SearchOrder:=xlRows, _
                             SearchDirection:=xlPrevious, _
                             LookIn:=xlValues).Row + 1
    'check for a Trivia Question
    If Trim(Me.TxtQNum.Value) = "" Then
      Me.TxtQNum.SetFocus
      MsgBox "Please enter a Trivia Question"
      Exit Sub
    End If
    
    'clear the data
    Me.TxtQNum.Value = iRow - 1
    Me.TxtTrvQues.Value = ""
    Me.CBCat.Value = ""
    Me.CBDif.Value = ""
    Me.TxtTrvAns1 = ""
    Me.TxtTrvAns2 = ""
    Me.TxtTrvAns3 = ""
    Me.TxtTrvAns4 = ""
    Me.CBTrvAns1 = ""
    Me.CBTrvAns2 = ""
    Me.CBTrvAns3 = ""
    Me.CBTrvAns4 = ""
    Me.TxtTrvQues.SetFocus
    
    SaveQuestion.Visible = False
    SubQues.Visible = True
    
    End Sub
    
    
    Private Sub SpinButton1_SpinDown()
    Dim ws1 As Worksheet:   Set ws1 = Sheets("Questions")
    Dim ws2 As Worksheet:   Set ws2 = Sheets("Answers")
    Dim rFind As Range
    Dim r2Find As Range
    
    strQ = TxtQNum.Text
    strQu = TxtTrvAns1.Text
    
    Set rFind = ws1.Range("A1:A" & ws1.Range("A" & Rows.Count).End(xlUp).Row).Find(What:=strQ, LookIn:=xlValues, Lookat:=xlWhole)
    
    If Not rFind Is Nothing Then
        TxtQNum.Text = rFind.Offset(1, 0).Value
        TxtTrvQues.Text = rFind.Offset(1, 1).Value
        CBCat.Text = rFind.Offset(1, 2).Value
        CBDif = rFind.Offset(1, 3).Value
    End If
    
    Set r2Find = ws2.Range("A1:A" & ws2.Range("A" & Rows.Count).End(xlUp).Row).Find(What:=strQ, LookIn:=xlValues, Lookat:=xlWhole)
    
    If Not r2Find Is Nothing Then
        TxtTrvAns1.Text = r2Find.Offset(4, 1).Value
        TxtTrvAns2.Text = r2Find.Offset(5, 1).Value
        TxtTrvAns3.Text = r2Find.Offset(6, 1).Value
        TxtTrvAns4.Text = r2Find.Offset(7, 1).Value
        CBTrvAns1.Value = r2Find.Offset(4, 2).Value
        CBTrvAns2.Value = r2Find.Offset(5, 2).Value
        CBTrvAns3.Value = r2Find.Offset(6, 2).Value
        CBTrvAns4.Value = r2Find.Offset(7, 2).Value
          
    End If
    End Sub
    
    Private Sub SpinButton1_SpinUp()
    Dim ws1 As Worksheet:   Set ws1 = Sheets("Questions")
    Dim ws2 As Worksheet:   Set ws2 = Sheets("Answers")
    Dim rFind As Range
    Dim r2Find As Range
    
    strQ = TxtQNum.Text
    strQu = TxtTrvAns1.Text
    Set rFind = ws1.Range("A1:A" & ws1.Range("A" & Rows.Count).End(xlUp).Row).Find(What:=strQ, LookIn:=xlValues, Lookat:=xlWhole)
    
    If Not rFind Is Nothing Then
        TxtQNum.Text = rFind.Offset(-1, 0).Value
        TxtTrvQues.Text = rFind.Offset(-1, 1).Value
        CBCat.Text = rFind.Offset(-1, 2).Value
        CBDif.Text = rFind.Offset(-1, 3).Value
    End If
    
    Set r2Find = ws2.Range("A1:A" & ws2.Range("A" & Rows.Count).End(xlUp).Row).Find(What:=strQ, LookIn:=xlValues, Lookat:=xlWhole)
    
    If Not r2Find Is Nothing Then
        TxtTrvAns1.Text = r2Find.Offset(-4, 1).Value
        TxtTrvAns2.Text = r2Find.Offset(-3, 1).Value
        TxtTrvAns3.Text = r2Find.Offset(-2, 1).Value
        TxtTrvAns4.Text = r2Find.Offset(-1, 1).Value
        CBTrvAns1.Value = r2Find.Offset(-4, 2).Value
        CBTrvAns2.Value = r2Find.Offset(-3, 2).Value
        CBTrvAns3.Value = r2Find.Offset(-2, 2).Value
        CBTrvAns4.Value = r2Find.Offset(-1, 2).Value
          
        
    End If
    End Sub
    
    Private Sub SubQues_Click()
    Dim iRow As Long
    Dim iaRow As Long
    Dim ws As Worksheet
    Dim ans As Worksheet
    Set ws = Worksheets("Questions")
    Set ans = Worksheets("Answers")
    
    'find first empty row in database
      iRow = ws.Cells.Find(What:="*", _
                             SearchOrder:=xlRows, _
                             SearchDirection:=xlPrevious, _
                             LookIn:=xlValues).Row + 1
                             
    'find first empty row in database
      iaRow = ans.Cells.Find(What:="*", _
                             SearchOrder:=xlRows, _
                             SearchDirection:=xlPrevious, _
                             LookIn:=xlValues).Row + 1
                             
    'check for a Trivia Question
    If Trim(Me.TxtQNum.Value) = "" Then
      Me.TxtQNum.SetFocus
      MsgBox "Please enter a Trivia Question"
      Exit Sub
    End If
    
    'copy the data to the database
    'use protect and unprotect lines,
    
    With ws
    
      .Cells(iRow, 1).Value = Me.TxtQNum.Value
      .Cells(iRow, 2).Value = Me.TxtTrvQues.Value
      .Cells(iRow, 3).Value = Me.CBCat.Value
      .Cells(iRow, 4).Value = Me.CBDif.Value
      
    
    End With
    
    With ans
    
      .Cells(iaRow, 1).Value = Me.TxtTrvAns1.Value
      .Cells(iaRow + 1, 1).Value = Me.TxtTrvAns2.Value
      .Cells(iaRow + 2, 1).Value = Me.TxtTrvAns3.Value
      .Cells(iaRow + 3, 1).Value = Me.TxtTrvAns4.Value
      .Cells(iaRow, 2).Value = -Me.CBTrvAns1.Value
      .Cells(iaRow + 1, 2).Value = -Me.CBTrvAns2.Value
      .Cells(iaRow + 2, 2).Value = -Me.CBTrvAns3.Value
      .Cells(iaRow + 3, 2).Value = -Me.CBTrvAns4.Value
    
    
    End With
    
    SaveQuestion.Visible = True
    SubQues.Visible = False
    'Show that question was submitted
      MsgBox "Your question has been added"
    End Sub
    
    Private Sub TxtQNum_Change()
    
    End Sub
    
    Private Sub TxtTrvAns1_Change()
    
    End Sub
    
    Private Sub TxtTrvQues_Change()
    
    End Sub
    
    Private Sub UserForm_Initialize()
        Dim TrvQues As Worksheet
        Dim TrvAns As Worksheet
        Dim TrvSet As Range
        Dim TrvDif As Range
        Dim ts As Worksheet
        
      Set TrvQues = Worksheets("Questions")
      Set TrvAns = Worksheets("Answers")
      Set ts = Worksheets("Settings")
      
    For Each TrvSet In ts.Range("Catagories")
        With Me.CBCat
            .AddItem TrvSet.Value
        End With
    Next TrvSet
    
    For Each TrvDif In ts.Range("Difficulty")
        With Me.CBDif
            .AddItem TrvDif.Value
        End With
    Next TrvDif
    
    
    
    Me.TxtQNum = TrvQues.Range("A2").Value
    Me.TxtTrvQues = TrvQues.Range("B2").Value
    Me.CBCat = TrvQues.Range("C2").Value
    Me.CBDif = TrvQues.Range("D2").Value
    Me.TxtTrvAns1 = TrvAns.Range("B2").Value
    Me.TxtTrvAns2 = TrvAns.Range("B3").Value
    Me.TxtTrvAns3 = TrvAns.Range("B4").Value
    Me.TxtTrvAns4 = TrvAns.Range("B5").Value
    Me.CBTrvAns1 = TrvAns.Range("C2").Value
    Me.CBTrvAns2 = TrvAns.Range("C3").Value
    Me.CBTrvAns3 = TrvAns.Range("C4").Value
    Me.CBTrvAns4 = TrvAns.Range("C5").Value
    
    SubQues.Visible = False
    
    
    
    End Sub
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Can not run SQL update statement
    By jsabo in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-22-2013, 07:45 PM
  2. VBA and SQL - help with update statement
    By jsabo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-21-2013, 06:57 PM
  3. IIF statement in SQL Update query in VBA
    By mhm in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-14-2013, 08:23 PM
  4. Syntax Error in UPDATE Statement ?
    By AnthonyWB in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-02-2011, 11:06 AM
  5. VBA SQL update statement
    By shawby in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 11-29-2010, 08:41 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1