我写了一些代码并得到了两个错误:
'Else' must be preceded by a matching 'If' or 'ElseIf'.
Expression expected.
以下是代码:
Imports System.Data.Odbc
Public Class FormLogin
Dim CMD As New OdbcCommand
Dim RD As OdbcDataReader
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox1.Text = "" Or TextBox2.Text = "" Then MsgBox("Data Login Belum Lengkap!")
Exit Sub
Else
Call Koneksi()
CMD = New OdbcCommand("Select * From tbl_admin where kode_admin=" & TextBox1.Text & "and password_admin=" & TextBox2.Text &, Conn)
RD = CMD.ExecuteReader
RD.Read()
If RD.HasRows Then
Me.Close()
FormMenuUtama.Show()
FormMenuUtama.LoginToolStripMenuItem.Enabled = False
FormMenuUtama.LogoutToolStripMenuItem.Enabled = True
FormMenuUtama.MasterToolStripMenuItem.Enabled = True
FormMenuUtama.TransaksiToolStripMenuItem.Enabled = True
FormMenuUtama.LaporanToolStripMenuItem.Enabled = True
Else
MsgBox("Kode Admin atau Password Salah")
End If
End Sub
Private Sub FormLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.MaxLength = 6
TextBox2.PasswordChar = "*"
TextBox1.Clear()
TextBox2.Clear()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
End Class
答案 0 :(得分:1)
1)不要将Then
之后的代码放在If
语句中。它变成了一行
所以:
If TextBox1.Text = "" Or TextBox2.Text = "" Then
MsgBox("Data Login Belum Lengkap!")
...
而不是
If TextBox1.Text = "" Or TextBox2.Text = "" Then MsgBox("Data Login Belum Lengkap!")
...
2)还有一个额外的&
所以:
CMD = New OdbcCommand("Select * From tbl_admin where kode_admin=" & TextBox1.Text & "and password_admin=" & TextBox2.Text , Conn)
而不是
CMD = New OdbcCommand("Select * From tbl_admin where kode_admin=" & TextBox1.Text & "and password_admin=" & TextBox2.Text &, Conn)
答案 1 :(得分:-1)
在End Sub。
之前添加另一个End If Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox1.Text = "" Or TextBox2.Text = "" Then MsgBox("Data Login Belum Lengkap!")
Exit Sub
Else
Call Koneksi()
CMD = New OdbcCommand("Select * From tbl_admin where kode_admin=" & TextBox1.Text & "and password_admin=" & TextBox2.Text &, Conn)
RD = CMD.ExecuteReader
RD.Read()
If RD.HasRows Then
Me.Close()
FormMenuUtama.Show()
FormMenuUtama.LoginToolStripMenuItem.Enabled = False
FormMenuUtama.LogoutToolStripMenuItem.Enabled = True
FormMenuUtama.MasterToolStripMenuItem.Enabled = True
FormMenuUtama.TransaksiToolStripMenuItem.Enabled = True
FormMenuUtama.LaporanToolStripMenuItem.Enabled = True
Else
MsgBox("Kode Admin atau Password Salah")
End If
End If
End Sub