Solved

VB6 SQL Query

Posted on 2014-11-25
4
204 Views
Last Modified: 2014-11-26
I am writing VB6 for Email2DB script at my company and cannot seem to figure stuff out. I am new to VB6 I can do C# and its been a while since VB.net

Here is what I am looking to do.

I want to connect to my SQLEXPRESS server and run a query to see if something exists. Something like.

SELECT USERNAME FROM MFUSERS WHERE USERNAME= MSG.GetToAddr(i).

Then assign the result to a string

So Dim result as string = sql.response   (not sure how to code this either)

Then am having my vb script check if the MSG.GetToAddr = the sql.response. If so it does something. If not it does something else.

Here is my code so far.


' Outbound Mail Server
' This script fires before emails are sent via the Email2DB Mail Server.

' Use it to implement custom accept/reject rules, or change/add headers or message text.
' Set the property Email2DBAccept to True to accept the message.
' The current message can be accessed via the MSG object.

Sub Main()

    ' commands start here
 Email2DBAccept = Email2DBAccept
Dim numto As Long
numto = MSG.NumTo

If (numto > 0) Then

    For i = 0 To numto - 1
    'This checks to see if the email exists in the DB if it does then it does not relay it.
    'If it does not exists then it sends it normal and allows it to relay out.
    If MSG.GetToAddr(i) ="test@balls.com" Then
            Email2DBAccept =False
    Else
            Email2DBAccept=True


    End If


    Next
' This is the final end if below which has the starting of the If (numto) in the begining
End If
End Sub

Open in new window

0
Comment
Question by:desiredforsome
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 11

Accepted Solution

by:
jkpieterse earned 500 total points
ID: 40466349
Typically you would use the ADO library to do this. There are gazillions examples out there aimed at Excel VBA which you can easily use in your VB6 project. The site http://erlandsendata.no/?cat=15 has connection strings for most databases.
0
 
LVL 85
ID: 40466358
What are you having trouble with? As jkpieterse said, you use ADO to connect to SQL Server - are you able to do that? Basically, it's something like:

Dim con As New ADODB.Connection
con.ConnectionString = "Your connection string"
con.Open
'/ if you are returning an integer value, perhaps:
Dim cnt As Integer
cnt - con.execute("SELECT Count(*) AS RecCount FROM SomeTable")
'/ if you need to open a Recordset:
Dim rst As ADODB.Recordset
rst.Open "SELECT * FROM SomeTable WHERE MyIDField=" & txSomeFieldOnMyForm, con

If Not (rst.EOF and rst.BOF) Then
  Msgbox rst("SomeField")
End If
0
 

Author Closing Comment

by:desiredforsome
ID: 40466975
Further research got me where i wanted.
0
 
LVL 40

Expert Comment

by:Vadim Rapp
ID: 40467262
Dim cn As New ADODB.Connection(<connection string>)
cn.open
if cn.execute("select 1 from users where username='" & myvariable & "'").eof then
  msgbox "user does not exist"
else
  msgbox "user exists"
end if
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question