Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB How-To
|
VB How-To Ask your "How do I do this with VB?" questions in this forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 2nd, 2007, 03:16 PM
Registered User
 
Join Date: Apr 2007
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to jscammell
Default Connect to Remote SQL database via Code

How do I code for a connection to a remote SQL server/database file, then add a new row of information and close the connection?

Alternately, how do I connect to the database to view the data in a datagrid, then close the connection?

Purposes; a database that takes data, and stores it for use later for reporting and quality purposes. I would use the New Data Source wizard, but it won't connect to remote databases, and I'm already in over my head...here is an example of the code for the program that inputs the data to the data base:

Public Class frmMain

    Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.Table1TableAdapter.Fill(Me.Tracker1DataSet.Tabl e1)
'TODO: This line of code loads data into the 'Tracker1DataSet.Table1' table. You can move, or remove it, as needed.

    Private Sub cboReasonCode_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboReasonCode.Leave
        If cboReasonCode.Text = "" Then
            MsgBox("Please Enter a Reason Code for to Continue.")
            Me.cboReasonCode.Focus()
        End If
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Close()
    End Sub



    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

        txtCSAID.Text = ""
        cboOU.Text = ""
        cboOU.SelectedItem = vbNullString
        cboReasonCode.Text = ""
        cboReasonCode.SelectedItem = vbNullString
        dtpDate.Value = Now
        txtCustID.Text = ""
        txtNo.Text = ""
        txtNotes.Text = ""
        ckbCSNet.CheckState = CheckState.Unchecked
        ckbBlurbDB.CheckState = CheckState.Unchecked
        ckbWizard.CheckState = CheckState.Unchecked
        ckbWebsite.CheckState = CheckState.Unchecked
        ckbHelpPages.CheckState = CheckState.Unchecked
        ckbKnowledgeCenter.CheckState = CheckState.Unchecked
        ckbEmail.CheckState = CheckState.Unchecked
        ckbTechCSNet.CheckState = CheckState.Unchecked
        ckbSharepoint.CheckState = CheckState.Unchecked
        ckbHandouts.CheckState = CheckState.Unchecked
        ckbInquiry.CheckState = CheckState.Unchecked
        ckbEscalation.CheckState = CheckState.Unchecked
        ckbEscAppYes.CheckState = CheckState.Unchecked
        ckbEscAppNo.CheckState = CheckState.Unchecked
        txtCSAID.Focus()
    End Sub

    Private Sub txtCSAID_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtCSAID.Leave
        dtpDate.Value = Now
        If txtCSAID.Text = "" Then
            MsgBox("Please Enter a CSA Log In to Continue.")
            Me.txtCSAID.Focus()
        ElseIf txtCSAID.Text.Length < 2 Then
            MsgBox("Please Enter a Valid CSA Log In to Continue.")
            Me.txtCSAID.Focus()
        End If

    End Sub

    Private Sub txtCustID_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtCustID.Leave
        If txtCustID.Text = "" Then
            MsgBox("Please Enter a Customer ID to Continue.")
            Me.txtCustID.Focus()
        ElseIf txtCustID.Text.Length < 4 Then
            MsgBox("Please Enter a Valid Customer ID to Continue.")
            Me.txtCustID.Focus()
        End If

    End Sub

    Private Sub btnSubmit_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Dim strWebsite As String
        Dim strHelpPages As String
        Dim strCSNet As String
        Dim strBlurbDB As String
        Dim strWizard As String
        Dim strKnowCent As String
        Dim strEmail As String
        Dim strTechCSNet As String
        Dim strSharepoint As String
        Dim strHandouts As String
        Dim strInquiry As String
        Dim strEscalation As String
        Dim strEscApp As String

        If txtCustID.Text = "" Then
            MsgBox("Please Enter a Customer ID Before You Submit This Record.")
            Me.txtCustID.Focus()
        End If
        If txtCustID.Text.Length < 4 Then
            MsgBox("Please Enter a Valid Customer ID Before You Submit This Record.")
            Me.txtCustID.Focus()
        End If

        If txtCSAID.Text = "" Then
            MsgBox("Please Enter a CSA Log In Before You Submit This Record.")
            Me.txtCSAID.Focus()
        End If
        If txtCSAID.Text.Length < 2 Then
            MsgBox("Please Enter a Valid CSA Log In Before You Submit This Record.")
            Me.txtCSAID.Focus()
        End If

        If cboReasonCode.Text = "" Then
            MsgBox("Please Enter a Reason Code Before You Submit This Record.")
            Me.cboReasonCode.Focus()
        End If

        If cboOU.Text = "" Then
            MsgBox("Please Enter an OU Before You Submit This Record.")
            Me.cboOU.Focus()
        End If

        If Not txtOrder.Text = "" Then
            If Not txtOrder.Text.Length = 19 Then
                MsgBox("Please Enter the Dashes in the Order ID.")
                Me.txtOrder.Focus()
            End If
        End If
        If txtOrder.Text = "" Then
            txtOrder.Text = "N/A"
        End If
        If ckbCSNet.CheckState = CheckState.Checked Then
            strCSNet = "Y"
        ElseIf ckbCSNet.CheckState = CheckState.Unchecked Then
            strCSNet = "U"
        End If
        If ckbBlurbDB.CheckState = CheckState.Checked Then
            strBlurbDB = "Y"
        ElseIf ckbBlurbDB.CheckState = CheckState.Unchecked Then
            strBlurbDB = "U"
        End If
        If ckbWizard.CheckState = CheckState.Checked Then
            strWizard = "Y"
        ElseIf ckbWizard.CheckState = CheckState.Unchecked Then
            strWizard = "U"
        End If
        If ckbWebsite.CheckState = CheckState.Checked Then
            strWebsite = "Y"
        ElseIf ckbWebsite.CheckState = CheckState.Unchecked Then
            strWebsite = "U"
        End If
        If ckbHelpPages.CheckState = CheckState.Checked Then
            strHelpPages = "Y"
        ElseIf ckbHelpPages.CheckState = CheckState.Unchecked Then
            strHelpPages = "U"
        End If
        If ckbKnowledgeCenter.CheckState = CheckState.Checked Then
            strKnowCent = "Y"
        ElseIf ckbKnowledgeCenter.CheckState = CheckState.Unchecked Then
            strKnowCent = "U"
        End If
        If ckbEmail.CheckState = CheckState.Checked Then
            strEmail = "Y"
        ElseIf ckbEmail.CheckState = CheckState.Unchecked Then
            strEmail = "U"
        End If
        If ckbTechCSNet.CheckState = CheckState.Checked Then
            strTechCSNet = "Y"
        ElseIf ckbTechCSNet.CheckState = CheckState.Unchecked Then
            strTechCSNet = "U"
        End If
        If ckbSharepoint.CheckState = CheckState.Checked Then
            strSharepoint = "Y"
        ElseIf ckbSharepoint.CheckState = CheckState.Unchecked Then
            strSharepoint = "U"
        End If
        If ckbHandouts.CheckState = CheckState.Checked Then
            strHandouts = "Y"
        ElseIf ckbHandouts.CheckState = CheckState.Unchecked Then
            strHandouts = "U"
        End If
        If ckbInquiry.CheckState = CheckState.Checked Then
            strInquiry = "Y"
        ElseIf ckbInquiry.CheckState = CheckState.Unchecked Then
            strInquiry = "U"
        End If
        If ckbEscalation.CheckState = CheckState.Checked Then
            strEscalation = "Y"
        ElseIf ckbEscalation.CheckState = CheckState.Unchecked Then
            strEscalation = "U"
        End If
        If ckbEscAppYes.CheckState = CheckState.Checked Then
            strEscApp = "Y"
        ElseIf ckbEscAppNo.CheckState = CheckState.Checked Then
            strEscApp = "N"
        Else
            strEscApp = "U"
        End If
        If strInquiry = "U" Then
            If strEscalation = "U" Then
                MsgBox("Please Enter The CSA Contact Reason")
                Me.ckbInquiry.Focus()
            End If
        End If
        If txtNo.Text = "" Then
            txtNo.Text = "N/A"
        End If
        If txtNotes.Text = "" Then
            txtNotes.Text = "N/A"
        End If

        Me.Validate()
        Me.Table1TableAdapter.Insert(frmLogIn.txtLeadID.Te xt, txtCSAID.Text, txtCustID.Text, txtOrder.Text, cboOU.Text, cboReasonCode.Text, dtpDate.Value, strCSNet, strBlurbDB, strWizard, strHelpPages, strKnowCent, strWebsite, strEmail, strTechCSNet, strSharepoint, strHandouts, strInquiry, strEscalation, strEscApp, txtNo.Text, txtNotes.Text)
        MsgBox("Update successful")

        txtCSAID.Text = ""
        cboOU.Text = ""
        cboOU.SelectedItem = vbNullString
        cboReasonCode.Text = ""
        cboReasonCode.SelectedItem = vbNullString
        dtpDate.Value = Now
        txtNo.Text = ""
        txtNotes.Text = ""
        txtCustID.Text = ""
        txtOrder.Text = ""
        ckbCSNet.CheckState = CheckState.Unchecked
        ckbBlurbDB.CheckState = CheckState.Unchecked
        ckbWizard.CheckState = CheckState.Unchecked
        ckbWebsite.CheckState = CheckState.Unchecked
        ckbHelpPages.CheckState = CheckState.Unchecked
        ckbKnowledgeCenter.CheckState = CheckState.Unchecked
        ckbEmail.CheckState = CheckState.Unchecked
        ckbTechCSNet.CheckState = CheckState.Unchecked
        ckbSharepoint.CheckState = CheckState.Unchecked
        ckbHandouts.CheckState = CheckState.Unchecked
        ckbInquiry.CheckState = CheckState.Unchecked
        ckbEscalation.CheckState = CheckState.Unchecked
        ckbEscAppYes.CheckState = CheckState.Unchecked
        ckbEscAppNo.CheckState = CheckState.Unchecked
        btnExit.Focus()


    End Sub



    Private Sub cboOU_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboOU.Leave
        If cboOU.Text = "" Then
            MsgBox("Please Enter an OU to continue.")
            Me.cboOU.Focus()
        End If
    End Sub

    Private Sub btnEndOfShift_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        frmSubmit.Show()
    End Sub

    Private Sub txtOrder_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtOrder.Leave
        Dim string1 As String
        Dim string2 As String
        Dim string3 As String
        Dim string4 As String

        If Not txtOrder.Text = "" Then
            If txtOrder.Text.Length < 17 Then
                MsgBox("Please Enter the Complete Order ID")
                txtOrder.Focus()
            ElseIf txtOrder.Text.Length = 17 Then
                string1 = txtOrder.Text.Remove(3, 14)
                string2 = txtOrder.Text.Remove(0, 3)
                string3 = string2.Remove(6, 7)
                string4 = txtOrder.Text.Remove(0, 10)
                txtOrder.Text = string1 & "-" & string3 & "-" & string4
            End If
        End If
    End Sub


    Private Sub ckbEscalation_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles ckbEscalation.Leave
        If ckbInquiry.CheckState = CheckState.Unchecked Then
            If ckbEscalation.CheckState = CheckState.Unchecked Then
                MsgBox("Please Enter The CSA Contact Reason")
                Me.ckbInquiry.Focus()
            End If
        End If
    End Sub

    Private Sub ckbEscAppNo_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles ckbEscAppNo.Leave
        If ckbEscalation.CheckState = CheckState.Checked Then
            If ckbEscAppYes.CheckState = CheckState.Unchecked Then
                If ckbEscAppNo.CheckState = CheckState.Unchecked Then
                    MsgBox("You Indicated This Contact is an Escalation, Please Indicate if This is an Appropriate Escalation.")
                    ckbEscAppYes.Focus()
                End If
            End If
        End If
    End Sub

    Private Sub txtNo_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtNo.Leave
        If ckbEscAppNo.CheckState = CheckState.Checked Then
            If txtNo.Text = "" Then
                MsgBox("Please Indicate Why This Escalation is Not Appropriate.")
                txtNo.Focus()
            End If
        End If
    End Sub

    Private Sub btnExit_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        End
    End Sub
End Class
 
Old May 20th, 2007, 12:00 AM
Friend of Wrox
 
Join Date: Dec 2004
Posts: 221
Thanks: 0
Thanked 0 Times in 0 Posts
Default

<How do I code for a connection to a remote SQL server/database file, then add a new row of information and close the connection?>

I don't see any problem in here. just use the remote sql server name or ip address of it and user id and password for authenticating... The code establishes the connection without any problem...

<Alternately, how do I connect to the database to view the data in a datagrid, then close the connection?>

The problem may come, only if you are connecting to internat via proxy, it doesn't works like that... In such case you have to run the code in internet server in your network...

Just consider... First connect to your local SQL server or MSDE, abd make the code ready for it.. when it is working, its fine. And It will also work for remote server. Only you have to change the SQL server name and stuff.. thats it...

I have a same scanerio.. but proxy was the problem in my case... and rest works fine.


With Regards,
Raghavendra Mudugal





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to connect a PDA to a remote mysql database? baburman .NET Framework 2.0 2 November 24th, 2008 12:59 PM
connect to remote acccess database aasheesh_jha Pro VB Databases 1 October 30th, 2007 01:57 PM
Connect Remote SQLServer Database krajdyal ADO.NET 1 December 21st, 2005 02:47 PM
connect from web server to remote database Durwood Edwards Classic ASP Databases 0 June 24th, 2004 01:03 PM
Connect to remote database sankar Pro VB Databases 0 July 31st, 2003 01:43 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.