Vba 从字符串“”到类型“Boolean”的转换无效

Vba 从字符串“”到类型“Boolean”的转换无效,vba,type-conversion,Vba,Type Conversion,如果从字符串到类型“Boolean”的转换无效,我会遇到问题 If xlstxtbox1.Text = "" & xlstxtbox2.Text = "" & xlstxtbox3.Text = "" Then cmdselcet = New OleDbCommand("select * from [sheet1$]",cn) '2 ElseIf xlstxtbox1.

如果从字符串到类型“Boolean”的转换无效,我会遇到问题

If xlstxtbox1.Text = "" & xlstxtbox2.Text = "" & xlstxtbox3.Text = "" Then
                    cmdselcet = New OleDbCommand("select * from [sheet1$]",cn)
                    '2
                ElseIf xlstxtbox1.Text <> "" & xlstxtbox2.Text = "" & xlstxtbox3.Text = "" Then
                    cmdselcet = New OleDbCommand("select " + xlstxtbox1.Text + "from [sheet1$] ", cn)
                    '3
                ElseIf xlstxtbox1.Text = "" & xlstxtbox2.Text <> "" & xlstxtbox3.Text = "" Then
                    cmdselcet = New OleDbCommand("select * from [" + xlstxtbox2.Text + "$] ", cn)
                    '4
                ElseIf xlstxtbox1.Text = "" & xlstxtbox2.Text = "" & xlstxtbox3.Text <> "" Then
                    cmdselcet = New OleDbCommand("select * from [sheet1$] " + xlstxtbox3.Text, cn)
                    '5
                ElseIf xlstxtbox1.Text <> "" & xlstxtbox2.Text <> "" & xlstxtbox3.Text = "" Then
                    cmdselcet = New OleDbCommand("select " + xlstxtbox1.Text + "from [" + xlstxtbox2.Text + "$] ", cn)
                    '6
                ElseIf xlstxtbox1.Text = "" & xlstxtbox2.Text <> "" & xlstxtbox3.Text <> "" Then
                    cmdselcet = New OleDbCommand("select * from [" + xlstxtbox2.Text + "$] " + xlstxtbox3.Text, cn)
                    '7
                ElseIf xlstxtbox1.Text <> "" & xlstxtbox2.Text = "" & xlstxtbox3.Text <> "" Then
                    cmdselcet = New OleDbCommand("select " + xlstxtbox1.Text + " from [sheet1$] " + xlstxtbox3.Text, cn)
                    '8
                ElseIf xlstxtbox1.Text <> "" & xlstxtbox2.Text <> "" & xlstxtbox3.Text <> "" Then
                    cmdselcet = New OleDbCommand("select " + xlstxtbox1.Text + " from [" + xlstxtbox2.Text + "$] " + xlstxtbox3.Text, cn)
                End If
这就是答案

私有子xls\u opnsender As Object,e As EventArgs处理openxls。单击

    Dim cmdselcet = New OleDbCommand

    Dim adapter As New OleDbDataAdapter

    Using cn As New OleDbConnection(constr)

        Try

            cn.Open()

            '1 üres,üres,üres
            If xlstxtbox1.Text.Length = 0 & xlstxtbox2.Text.Length = 0 & xlstxtbox3.Text.Length = 0 Then
                cmdselcet = New OleDbCommand("select * from [sheet1$]", cn)

                '2 nem,üres,üres
            ElseIf xlstxtbox1.Text.Length <> 0 & xlstxtbox2.Text.Length = 0 & xlstxtbox3.Text.Length = 0 Then
                cmdselcet = New OleDbCommand("select " + xlstxtbox1.Text + " from [sheet1$] ", cn)

                '3 üres,nem,üres
            ElseIf xlstxtbox1.Text.Length = 0 & xlstxtbox2.Text.Length <> 0 & xlstxtbox3.Text.Length = 0 Then
                cmdselcet = New OleDbCommand("select * from [" + xlstxtbox2.Text + "$] ", cn)

                '4 üres,üres,nem
            ElseIf xlstxtbox1.Text.Length = 0 & xlstxtbox2.Text.Length = 0 & xlstxtbox3.Text.Length <> 0 Then
                cmdselcet = New OleDbCommand("select * from [sheet1$] " + xlstxtbox3.Text, cn)

                '5 nem,nem,üres
            ElseIf xlstxtbox1.Text.Length <> 0 & xlstxtbox2.Text.Length <> 0 & xlstxtbox3.Text.Length = 0 Then
                cmdselcet = New OleDbCommand("select " + xlstxtbox1.Text + " from [" + xlstxtbox2.Text + "$] ", cn)

                '6 üres,nem,nem
            ElseIf xlstxtbox1.Text.Length = 0 & xlstxtbox2.Text.Length <> 0 & xlstxtbox3.Text.Length <> 0 Then
                cmdselcet = New OleDbCommand("select * from [" + xlstxtbox2.Text + "$] " + xlstxtbox3.Text, cn)

                '7 nem,üres,nem
            ElseIf xlstxtbox1.Text.Length <> 0 & xlstxtbox2.Text.Length = 0 & xlstxtbox3.Text.Length <> 0 Then
                cmdselcet = New OleDbCommand("select " + xlstxtbox1.Text + " from [sheet1$] " + xlstxtbox3.Text, cn)

                '8 nem,nem,nem
            ElseIf xlstxtbox1.Text.Length <> 0 & xlstxtbox2.Text.Length <> 0 & xlstxtbox3.Text.Length <> 0 Then
                cmdselcet = New OleDbCommand("select " + xlstxtbox1.Text + " from [" + xlstxtbox2.Text + "$] " + xlstxtbox3.Text, cn)
            End If

            adapter.SelectCommand = cmdselcet
            Dim ds As DataSet
            ds = New DataSet

            'Megjelenítés
            adapter.Fill(ds)
            DataGridView1.DataSource = ds.Tables(0)
            DataGridView1.Visible = True
            MsgBox("XLS/XLSX import kész!")

        Catch ex As Exception

            DataGridView1.Visible = False
            MsgBox(ex.Message)

        End Try

    End Using

End Sub

为什么您希望能够将空字符串转换为布尔值?也许您可以找出哪个表达式产生了错误。这是我的选择,但我有一个例外:从字符串到“Boolean”类型的转换无效。我刚写信回答这个问题,但被删除了。我只是想得到一个答案,我如何修复它。首先,找出你在代码中尝试将空字符串转换为布尔值的位置。在代码中的什么地方会发生这种情况?nwm,sry恕我冒昧,我发现的方法是这样的:xlstxtbox1.Text.length=0我看不到问题中的代码。这很令人困惑。