Avatar billede BIRGER Seniormester
26. juni 2004 - 09:36 Der er 13 kommentarer og
1 løsning

Problemer med if sætning i VBA word program

Jeg tror at jeg er kørt fast i if-sætninger i dette prog. Min fornemmelse er at det er i den sidste del af programmet...altså hvor der skal komme en msgbox..hvis tlfnr ikke findes! fejlmedd. siger...else without if
Jeg prøver at vedlægge programmet.

Sub kundetlf()
tlfnr = InputBox("indtast telefonnr")

If tlfnr = "" Then
Application.ScreenUpdating = False ActiveWindow.ActivePane.VerticalPercentScrolled = 0
 
Else

    Dim objConn As ADODB.Connection
    Dim objRs As ADODB.Recordset
    Dim strConnString As String
    Dim strSQL As String
   
    On Error Resume Next
    Set objConn = New ADODB.Connection
    Set objRs = New ADODB.Recordset
   
    strConnString = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=C:\dokumenter\Kartoteker.MDb"
    objConn.Open strConnString
   
    strSQL = "SELECT Kundenr,Navn1,navn2, Adresse, postby FROM kundekartotek WHERE tlfnr = " & tlfnr
    objRs.CursorType = adOpenStatic
    objRs.Open strSQL, objConn
 
  If objRs.RecordCount > 1 Then
Application.ScreenUpdating = False
A = objRs
UserForm1.ListBox1.ColumnCount = 5
  UserForm1.ListBox1.ColumnCount = 5
UserForm1.ListBox1.Clear
UserForm1.ListBox1.ColumnWidths = "35;140;140;140"

    If Not objRs.EOF Then
  i = 0
  Do While Not objRs.EOF
      With objRs
    UserForm1.ListBox1.AddItem objRs("Kundenr")
    UserForm1.ListBox1.List(i, 1) = objRs("Navn1")
    UserForm1.ListBox1.List(i, 2) = objRs("Navn2")
    UserForm1.ListBox1.List(i, 3) = objRs("adresse")
    UserForm1.ListBox1.List(i, 4) = objRs("postby")
       
        i = i + 1
        .MoveNext
      End With
    Loop

    UserForm1.Show
 
objRs.Close
   
  objConn.Close
  End If
 
Else
Application.ScreenUpdating = False
Selection.TypeText Text:="Kundenr: " & objRs("Kundenr") & vbCrLf
Selection.TypeText Text:="" & objRs("Navn1") & vbCrLf
Selection.TypeText Text:="" & objRs("Adresse") & vbCrLf
Selection.TypeText Text:="" & objRs("postby") & vbCrLf
  Application.ScreenUpdating = False
  Application.ScreenUpdating = False
  ActiveWindow.ActivePane.VerticalPercentScrolled = 0
ActiveDocument.Sections(6).ProtectedForForms = True
ActiveDocument.Protect Password:="", NoReset:=False, Type:= _
      wdAllowOnlyFormFields

objRs.Close
    objConn.Close
Else
MsgBox "Kundenr. findes ikke"
    ActiveDocument.Unprotect
    Application.ScreenUpdating = False
Selection.GoTo What:=wdGoToLine, Which:=wdGoToFirst, Count:=20, Name:=""
  Selection.Find.ClearFormatting
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    Selection.MoveRight Unit:=wdCell
    ActiveDocument.Sections(1).ProtectedForForms = False
ActiveDocument.Sections(2).ProtectedForForms = True
    ActiveDocument.Sections(3).ProtectedForForms = True
    ActiveDocument.Sections(4).ProtectedForForms = True
    ActiveDocument.Sections(5).ProtectedForForms = True
    ActiveDocument.Protect Password:="", NoReset:=False, Type:= _
        wdAllowOnlyFormFields
    ActiveWindow.ActivePane.VerticalPercentScrolled = 0
end If
End If
End Sub
Avatar billede ulrikp Nybegynder
26. juni 2004 - 09:55 #1
Du har en Else for meget i.f.t. If - som fejlmeddelelsen siger (-:
Din Else lige inden MsgBox "Kundenr. findes ikke" hører ikke til nogen If.
Avatar billede terry Ekspert
26. juni 2004 - 10:15 #2
THis gives a clean compile, but without knowing what the code is supposed to do I cant be sure that the if's and else's are in the correct position


Sub kundetlf()
tlfnr = InputBox("indtast telefonnr")

If tlfnr = "" Then
    Application.ScreenUpdating = False
    ActiveWindow.ActivePane.VerticalPercentScrolled = 0
 
Else

    Dim objConn As ADODB.Connection
    Dim objRs As ADODB.Recordset
    Dim strConnString As String
    Dim strSQL As String
   
    On Error Resume Next
    Set objConn = New ADODB.Connection
    Set objRs = New ADODB.Recordset
   
    strConnString = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=C:\dokumenter\Kartoteker.MDb"
    objConn.Open strConnString
   
    strSQL = "SELECT Kundenr,Navn1,navn2, Adresse, postby FROM kundekartotek WHERE tlfnr = " & tlfnr
    objRs.CursorType = adOpenStatic
    objRs.Open strSQL, objConn
 
    If objRs.RecordCount > 1 Then
        Application.ScreenUpdating = False
        A = objRs
        UserForm1.ListBox1.ColumnCount = 5
        UserForm1.ListBox1.ColumnCount = 5
        UserForm1.ListBox1.Clear
        UserForm1.ListBox1.ColumnWidths = "35;140;140;140"

        If Not objRs.EOF Then
            i = 0
            Do While Not objRs.EOF
                With objRs
                    UserForm1.ListBox1.AddItem objRs("Kundenr")
                    UserForm1.ListBox1.List(i, 1) = objRs("Navn1")
                    UserForm1.ListBox1.List(i, 2) = objRs("Navn2")
                    UserForm1.ListBox1.List(i, 3) = objRs("adresse")
                    UserForm1.ListBox1.List(i, 4) = objRs("postby")
       
                    i = i + 1
                    .MoveNext
                End With
            Loop

            UserForm1.Show
            objRs.Close
            objConn.Close
        End If
 
    Else
        Application.ScreenUpdating = False
        Selection.TypeText Text:="Kundenr: " & objRs("Kundenr") & vbCrLf
        Selection.TypeText Text:="" & objRs("Navn1") & vbCrLf
        Selection.TypeText Text:="" & objRs("Adresse") & vbCrLf
        Selection.TypeText Text:="" & objRs("postby") & vbCrLf
        Application.ScreenUpdating = False
        Application.ScreenUpdating = False
        ActiveWindow.ActivePane.VerticalPercentScrolled = 0
        ActiveDocument.Sections(6).ProtectedForForms = True
        ActiveDocument.Protect Password:="", NoReset:=False, Type:= _
        wdAllowOnlyFormFields

        objRs.Close
        objConn.Close
       
    End If '<<<<< Set In
'>>>> Else ?<<<<<<

    MsgBox "Kundenr. findes ikke"
    ActiveDocument.Unprotect
    Application.ScreenUpdating = False
    Selection.GoTo What:=wdGoToLine, Which:=wdGoToFirst, Count:=20, Name:=""
    Selection.Find.ClearFormatting

    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        Selection.MoveRight Unit:=wdCell
        ActiveDocument.Sections(1).ProtectedForForms = False
        ActiveDocument.Sections(2).ProtectedForForms = True
        ActiveDocument.Sections(3).ProtectedForForms = True
        ActiveDocument.Sections(4).ProtectedForForms = True
        ActiveDocument.Sections(5).ProtectedForForms = True
        ActiveDocument.Protect Password:="", NoReset:=False, Type:= _
        wdAllowOnlyFormFields
        ActiveWindow.ActivePane.VerticalPercentScrolled = 0

    End With '<<<<< set in
End If
'>>>> End If ? <<<<<

End Sub
Avatar billede terry Ekspert
26. juni 2004 - 10:18 #3
You can see where I have cahnged the code with >>> and <<<

Also notice how the code is formatted with TABS. This makes it more readable and easier to see errors
Avatar billede BIRGER Seniormester
26. juni 2004 - 11:13 #4
Jeg har sat den  'end if' ind men den siger at der mangler stadig en if til den sidste else...jeg forstår det ikke rigtigte ?Det ser ud til at koden kører indtil den sidste else...
Avatar billede terry Ekspert
26. juni 2004 - 12:43 #5
Have you tried this 26/06-2004 10:18:25?
Avatar billede BIRGER Seniormester
26. juni 2004 - 14:25 #6
ja jeg har prøvet at sætte din rettelser ind
Avatar billede BIRGER Seniormester
27. juni 2004 - 09:03 #7
Jeg er åbenbart kørt helt fast...den første If Then......skal den ikke have de 3 næste else "under sig"?  Det er måske det loop der slår det i stykker?
Avatar billede terry Ekspert
27. juni 2004 - 12:22 #8
the original question was concerning an ERROR "...else without if". Do you still have this error, or is it now a program logic problem you have?
Avatar billede terry Ekspert
27. juni 2004 - 12:31 #9
If it is a program logic problem, then I would suggest that you debug your way through the program so that you can see exactly what is happening. Without knowing exactly what it is your program is supposed to do it isnt easy to tell you what is wrong, but I dont understand why you have this else! If there is no record then there is no data in the recordset!

Else
        Application.ScreenUpdating = False
        Selection.TypeText Text:="Kundenr: " & objRs("Kundenr") & vbCrLf
  >>>  Selection.TypeText Text:="" & objRs("Navn1") & vbCrLf
  >>>  Selection.TypeText Text:="" & objRs("Adresse") & vbCrLf
  >>>  Selection.TypeText Text:="" & objRs("postby") & vbCrLf
        Application.ScreenUpdating = False
        Application.ScreenUpdating = False
        ActiveWindow.ActivePane.VerticalPercentScrolled = 0
        ActiveDocument.Sections(6).ProtectedForForms = True
        ActiveDocument.Protect Password:="", NoReset:=False, Type:= _
        wdAllowOnlyFormFields

        objRs.Close
        objConn.Close
Avatar billede BIRGER Seniormester
27. juni 2004 - 16:13 #10
den bliver ved med at der et else unden if...og det er det sidste else...altså hvor der skal komme en msgbox...mærkeligt...ellers er jeg bare ved at være lidt forvirret!! Hvis jeg slette den del sidste del af koden..altså fra det sidste else..så kører det!!
Avatar billede terry Ekspert
27. juni 2004 - 17:08 #11
you must be doing something wrong! If you take the code EXACTLY as it is it should
NOT give an error.
Avatar billede BIRGER Seniormester
27. juni 2004 - 17:48 #12
Jeg prøver videre...foreløbig tak!!
Avatar billede terry Ekspert
27. juni 2004 - 20:00 #13
selv tak

Did you get the code working without an error?
Avatar billede BIRGER Seniormester
27. juni 2004 - 21:41 #14
ja men ikke så det hele virkede efter hensigten, men jeg prøver lige at sende et nyt spørgsmål...
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester