If you're using SQLExpress you have to specify that as part of the SQL instance name.
For example: "Server = WH306UT\SQLExpress"
I just ran a test using your code, but specifying my machine and SQL instance successfully.
See code below, change server and instance name to match your system (of course!):
Private Sub CreateDSN()
' NOTE: This procedure creates a User DSN, **NOT** a System DSN
'
Dim strDriver As String
Dim strAttributes As String
strDriver = "SQL Server"
Dim catalog As String = "Hospital_DSN"
'
' NOTE THE INSTANCE NAME
strAttributes = "SERVER=WH306UT\SQLExpress" & Chr(0)
'strAttributes = "SERVER=" & machName & "" & Chr(0)
'
' THESE ARE YOUR CONNECTION ATTRIBUTES BELOW
strAttributes = strAttributes & "DSN=" & catalog & Chr(0)
strAttributes = strAttributes & "DESCRIPTION=DSN For HOSPITAL" & Chr(0)
strAttributes = strAttributes & "DATABASE=TEST_DB" & Chr(0)
strAttributes = strAttributes & "TRUSTED_CONNECTION=YES" & Chr(0)
' IF YOU WANT TO SEE THE ODBCAD32.exe DIALOG
' PASS THE WINDOW HANDLE OF THIS FORM AS THE FIRST ARGUMENT
' EX: SQLConfigDataSourceW(Me.Handle, 1, "SQL Server", strAttributes)
'
Dim iReturn As Integer = PInvokeImports.SQLConfigDataSourceW(CType(0, IntPtr), _
1, "SQL Server", strAttributes)
If CBool(iReturn) Then
MsgBox("DSN setup complete successfully.", _
CType(MsgBoxStyle.OkOnly + MsgBoxStyle.Information, MsgBoxStyle))
Else
MsgBox("DSN setup can not complete successfully.")
End If
End Sub
PInvokeImports Class (in a separate code file):
Imports System.Runtime.InteropServices
Public Class PInvokeImports
'Const ODBC_ADD_DSN = 1 ' Add data source
'Const ODBC_CONFIG_DSN = 2 ' Configure (edit) data source
'Const ODBC_REMOVE_DSN = 3 ' Remove data source
<DllImport("ODBCCP32.dll", CallingConvention:=CallingConvention.Winapi, _
CharSet:=CharSet.Unicode, SetLastError:=True)> _
Public Shared Function SQLConfigDataSourceW(ByVal hwndParent As IntPtr, _
ByVal fRequest As Integer, _
ByVal lpszDriver As String, _
ByVal lpszAttributes As String) _
As Integer
End Function
End Class