Solved

VB6 SQL Query

Posted on 2014-11-25
4
195 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
4 Comments
 
LVL 11

Accepted Solution

by:
jkpieterse earned 500 total points
Comment Utility
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 84
Comment Utility
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
Comment Utility
Further research got me where i wanted.
0
 
LVL 40

Expert Comment

by:Vadim Rapp
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

771 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now